Tim*_*Tim 21 generics pointers go
既然类型参数可以在 上使用golang/go:master,我决定尝试一下。看来我遇到了在类型参数提案中找不到的限制。(或者我一定错过了)。
我想编写一个函数,它返回带有接口类型约束的泛型类型值的切片。如果传递的类型是带有指针接收器的实现,我们如何实例化它?
type SetGetter[V any] interface {
    Set(V)
    Get() V
}
// SetGetterSlice turns a slice of type V into a slice of type T,
// with T.Set() called for each entry in values.
func SetGetterSlice[V any, T SetGetter[V]](values []V) []T {
    out := make([]T, len(values))
    for i, v := range values {
        out[i].Set(v) // panic if T has pointer receiver!
    }
    return out
}
当使用类型 as调用上述SetGetterSlice()函数时,此代码将在调用时出现混乱。(Go2go游乐场)毫不奇怪,因为基本上代码创建了一个指针切片:*CountTSet(v)nil
// Count implements SetGetter interface
type Count struct {
    x int
}
func (c *Count) Set(x int) { c.x = x }
func (c *Count) Get() int  { return c.x }
func main() {
    ints := []int{1, 2, 3, 4, 5}
    sgs := SetGetterSlice[int, *Count](ints)
    
    for _, s := range sgs {
        fmt.Println(s.Get())
    }
}
这个想法行不通,我似乎找不到任何简单的方法来实例化指向的值。
out[i] = new(T)将导致编译失败,因为它返回*T类型检查器想要查看的位置T。*new(T), 进行编译,但会导致相同的运行时恐慌,因为在这种情况下new(T)返回**Count,指向的指针Count仍然是nil。T导致编译失败:func SetGetterSlice[V any, T SetGetter[V]](values []V) []*T {
    out := make([]*T, len(values))
    for i, v := range values {
        out[i] = new(T)
        out[i].Set(v) // panic if T has pointer receiver
    }
    return out
}
func main() {
    ints := []int{1, 2, 3, 4, 5}
    SetGetterSlice[int, Count](ints)
    // Count does not satisfy SetGetter[V]: wrong method signature
}
到目前为止我找到的唯一解决方案是要求将构造函数传递给泛型函数。但这感觉不对,而且有点乏味。func F(T interface{})() []T如果这是完全有效的语法,为什么需要这样做?
func SetGetterSlice[V any, T SetGetter[V]](values []V, constructor func() T) []T {
    out := make([]T, len(values))
    for i, v := range values {
        out[i] = constructor()
        out[i].Set(v)
    }
    return out
}
// ...
func main() {
    ints := []int{1, 2, 3, 4, 5}
    SetGetterSlice[int, *Count](ints, func() *Count { return new(Count) })
}
我的问题,按优先顺序排列:
bla*_*een 26
基本上,您必须向约束添加一个类型参数,以使其可T转换为其指针类型。在最基本的形式中,该技术如下所示(带有匿名约束):
func Foo[T any, PT interface { *T; M() }]() {\n    p := PT(new(T))\n    p.M() // calling method on non-nil pointer\n}\n游乐场:https://go.dev/play/p/L00tePwrDfx
\n逐步解决方案
\n您的约束SetGetter已经声明了类型 param V,因此我们稍微修改上面的示例:
// V is your original type param\n// T is the additional helper param\ntype SetGetter[V any, T any] interface {\n    Set(V)\n    Get() V\n    *T\n}\n然后使用SetGetterSlice类型参数定义函数T any,其目的只是实例化约束SetGetter。
然后,您将能够将表达式转换&out[i]为指针类型,并成功调用指针接收器上的方法:
// T is the type with methods with pointer receiver\n// PT is the SetGetter constraint with *T\nfunc SetGetterSlice[V any, T any, PT SetGetter[V, T]](values []V) []T {\n    out := make([]T, len(values))\n\n    for i, v := range values {\n        // out[i] has type T\n        // &out[i] has type *T\n        // PT constraint includes *T\n        p := PT(&out[i]) // valid conversion!\n        p.Set(v)         // calling with non-nil pointer receiver\n    }\n\n    return out\n}\n完整程序:
\npackage main\n\nimport (\n    "fmt"\n)\n\ntype SetGetter[V any, T any] interface {\n    Set(V)\n    Get() V\n    *T\n}\n\nfunc SetGetterSlice[V any, T any, PT SetGetter[V, T]](values []V) []T {\n    out := make([]T, len(values))\n\n    for i, v := range values {\n        p := PT(&out[i])\n        p.Set(v)\n    }\n\n    return out\n}\n\n// Count implements SetGetter interface\ntype Count struct {\n    x int\n}\n\nfunc (c *Count) Set(x int) { c.x = x }\nfunc (c *Count) Get() int  { return c.x }\n\nfunc main() {\n    ints := []int{1, 2, 3, 4, 5}\n\n    // instantiate with base type\n    sgs := SetGetterSlice[int, Count](ints)\n\n    for _, s := range sgs {\n        fmt.Println(s.Get()) // prints 1,2,3,4,5 each in a newline\n    }\n}\n这变得更加冗长,因为SetGetterSlice现在需要三个类型参数:原始的Vplus T(带有指针接收器的类型)和PT(新的约束)。但是,当您调用该函数时,您可以通过类型推断省略第三个 \xe2\x80\x93 ,类型参数V和T实例化所需的参数PT SetGetter[V,T]都是已知的:
SetGetterSlice[int, Count](ints)\n游乐场:https://go.dev/play/p/gcQZnw07Wp3
\n