如何迭代在泛型函数中传递的切片联合? (T没有核心类型)
How to iterate over a union of slices passed in a generic function? (T has no core type)
我正在 go 1.18 中测试泛型并查看了这个 example。
我想重新创建该示例,但可以传入一片 int 或一片浮点数,而在函数中,我将对片中的所有内容求和。
这是我 运行 遇到一些问题时只是迭代切片。这是我试过的:
package main
import "fmt"
// NumberSlice constraint
type NumberSlice interface {
[]int64 | []float64
}
func add[N NumberSlice](n N) {
// want: to range over n and print value of v
for _, v := range n {
fmt.Println(v)
}
}
func main() {
ints := []int64{1, 2}
add(ints)
}
我收到错误:
cannot range over n (variable of type N constrained by NumberSlice) (N has no core type)
我该如何完成?
这样的事情对你有用吗?
package main
import "fmt"
type NumberOrFloat interface {
int64 | float64
}
func add[N NumberOrFloat](n []N) {
for _, v := range n {
fmt.Println(v)
}
}
func main() {
ints := []int64{1, 2}
add(ints)
}
这里的区别是你定义数组元素的类型约束(而不是数组类型):[]N
一个core type,对于一个接口(包括一个接口约束)定义如下:
An interface T has a core type if one of the following conditions is
satisfied:
There is a single type U
which is the underlying type of all types in the type set of T
or the type set of T contains only channel types with identical element type E, and all directional channels have the same direction.
你的接口约束没有核心类型,因为它有两个底层类型:[]int64
和[]float64
。
因此你不能在需要核心类型的地方使用它。特别是 range
和 make
.
您可以将接口更改为需要基本类型,然后在函数签名中指定切片:
// still no core type...
type Number interface {
int64 | float64
}
// ...but the argument will be instantiated with either int64 or float64
func add[N Number](n []N) {
for _, v := range n {
fmt.Println(v)
}
}
这也有效,但更冗长:
type NumberSlice[N int64 | float64] interface {
// one core type []N
~[]N
}
func add[S NumberSlice[N], N int64 | float64](n S) {
for _, v := range n {
fmt.Println(v)
}
}
我正在 go 1.18 中测试泛型并查看了这个 example。 我想重新创建该示例,但可以传入一片 int 或一片浮点数,而在函数中,我将对片中的所有内容求和。
这是我 运行 遇到一些问题时只是迭代切片。这是我试过的:
package main
import "fmt"
// NumberSlice constraint
type NumberSlice interface {
[]int64 | []float64
}
func add[N NumberSlice](n N) {
// want: to range over n and print value of v
for _, v := range n {
fmt.Println(v)
}
}
func main() {
ints := []int64{1, 2}
add(ints)
}
我收到错误:
cannot range over n (variable of type N constrained by NumberSlice) (N has no core type)
我该如何完成?
这样的事情对你有用吗?
package main
import "fmt"
type NumberOrFloat interface {
int64 | float64
}
func add[N NumberOrFloat](n []N) {
for _, v := range n {
fmt.Println(v)
}
}
func main() {
ints := []int64{1, 2}
add(ints)
}
这里的区别是你定义数组元素的类型约束(而不是数组类型):[]N
一个core type,对于一个接口(包括一个接口约束)定义如下:
An interface T has a core type if one of the following conditions is satisfied:
There is a single type
U
which is the underlying type of all types in the type set of Tor the type set of T contains only channel types with identical element type E, and all directional channels have the same direction.
你的接口约束没有核心类型,因为它有两个底层类型:[]int64
和[]float64
。
因此你不能在需要核心类型的地方使用它。特别是 range
和 make
.
您可以将接口更改为需要基本类型,然后在函数签名中指定切片:
// still no core type...
type Number interface {
int64 | float64
}
// ...but the argument will be instantiated with either int64 or float64
func add[N Number](n []N) {
for _, v := range n {
fmt.Println(v)
}
}
这也有效,但更冗长:
type NumberSlice[N int64 | float64] interface {
// one core type []N
~[]N
}
func add[S NumberSlice[N], N int64 | float64](n S) {
for _, v := range n {
fmt.Println(v)
}
}