This人曾在go1工作.18beta1,但不适用于go1.18rc1
package main
type A struct{}
func (*A) Hello() {
println("Hello")
}
func Create[M any, PT interface {
Hello()
*M
}](n int) (out []*M) {
for i := 0; i < n; i++ {
v := PT(new(M))
v.Hello()
out = append(out, v)
}
return
}
func main() {
println(Create[A](2))
}
我会扔
./prog.go:16:21: cannot use v (variable of type PT constrained by interface{Hello(); *M}) as type *M in argument to append:
PT does not implement *M (type *M is pointer to interface, not interface)
似乎是由于这个限制:
不允许将类型参数或指向类型参数的指针作为未命名字段嵌入 struct 类型中.同样,不允许在接口类型中嵌入类型参数.目前尚不清楚这些措施是否会被允许.
在go1我该怎么做.18rc1?