如何使用泛型 Go 实例化类型参数的非零指针?

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
}
Run Code Online (Sandbox Code Playgroud)

当使用类型 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())
    }
}
Run Code Online (Sandbox Code Playgroud)

同一问题的变体

这个想法行不通,我似乎找不到任何简单的方法来实例化指向的值。

  1. out[i] = new(T)将导致编译失败,因为它返回*T类型检查器想要查看的位置T
  2. 调用*new(T), 进行编译,但会导致相同的运行时恐慌,因为在这种情况下new(T)返回**Count,指向的指针Count仍然是nil
  3. 将返回类型更改为指针切片将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
}
Run Code Online (Sandbox Code Playgroud)

解决方法

到目前为止我找到的唯一解决方案是要求将构造函数传递给泛型函数。但这感觉不对,而且有点乏味。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) })
}
Run Code Online (Sandbox Code Playgroud)

概括

我的问题,按优先顺序排列:

  1. 我是否忽略了一些显而易见的事情?
  2. 这是 Go 中泛型的限制吗?这已经是最好的了吗?
  3. 此限制是否已知,或者我应该在 Go 项目中提出问题吗?

bla*_*een 26

基本上,您必须向约束添加一个类型参数,以使其可T转换为其指针类型。在最基本的形式中,该技术如下所示(带有匿名约束):

\n
func Foo[T any, PT interface { *T; M() }]() {\n    p := PT(new(T))\n    p.M() // calling method on non-nil pointer\n}\n
Run Code Online (Sandbox Code Playgroud)\n

游乐场:https://go.dev/play/p/L00tePwrDfx

\n
\n

逐步解决方案

\n

您的约束SetGetter已经声明了类型 param V,因此我们稍微修改上面的示例:

\n
// 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
Run Code Online (Sandbox Code Playgroud)\n

然后使用SetGetterSlice类型参数定义函数T any,其目的只是实例化约束SetGetter

\n

然后,您将能够将表达式转换&out[i]为指针类型,并成功调用指针接收器上的方法:

\n
// 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
Run Code Online (Sandbox Code Playgroud)\n

完整程序:

\n
package 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
Run Code Online (Sandbox Code Playgroud)\n

这变得更加冗长,因为SetGetterSlice现在需要三个类型参数:原始的Vplus T(带有指针接收器的类型)和PT(新的约束)。但是,当您调用该函数时,您可以通过类型推断省略第三个 \xe2\x80\x93 ,类型参数VT实例化所需的参数PT SetGetter[V,T]都是已知的:

\n
SetGetterSlice[int, Count](ints)\n
Run Code Online (Sandbox Code Playgroud)\n

游乐场:https://go.dev/play/p/gcQZnw07Wp3

\n