如何允许可转换为指针的泛型类型参数化另一个可转换为指针的泛型类型?

How does one allow a generic type that is convertible to a pointer to parameterise another generic type that is convertible to a pointer?

在类型参数提案中,a useful section 描述了应该如何定义类型约束,以便已经通过其指针接收器实现了接口的类型仍然可以用作类型参数,例如:

type ExplainedExampleGeneric[T any] interface {
    GetBool() bool
    *T // non-interface type constraint element
}

type ExplainedImpl struct{ bully bool }

func (e *ExplainedImpl) GetBool() bool { return e == nil || e.bully }

func Print[T any, PT ExplainedExampleGeneric[T]](impl T) {
    fmt.Println(PT(&impl).GetBool())
}

func main() {
    Print[ExplainedImpl](ExplainedImpl{}) // Prints: false
}

对于我的用例,我想使用这个 ExplainedExampleGeneric 作为另一个泛型类型的参数:

type UncharteredGeneric[T any, U any, V ExplainedExampleGeneric[U]] interface {
    GetString() string
    GetExplainedExampleGeneric() V
    *T // non-interface type constraint element
}

type ExplainedExampleGeneric[T any] interface {
    GetBool() bool
    *T // non-interface type constraint element
}

此编译,然而,当试图编译一个函数以利用此 UncharteredGeneric 类型时,我得到以下错误:

./prog.go:35:40: got 2 arguments but 3 type parameters
./prog.go:36:17: cannot convert &impl (value of type *T) to type PT
func UncharteredPrint[T any, U any, PT UncharteredGeneric[T, U]](impl T) { -> got 2 arguments but 3 type parameters
    fmt.Println(PT(&impl).GetExplainedExampleGeneric().GetBool()) -> cannot convert &impl (value of type *T) to type PT
}

最后,当我尝试调用这个函数时,无法推断出我的类型link to playground:

./prog.go:36:40: got 2 arguments but 3 type parameters
./prog.go:37:17: cannot convert &impl (value of type *T) to type PT
./prog.go:42:50: cannot infer PT (prog.go:36:37)
package main

import "fmt"

type UncharteredGeneric[T any, U any, V ExplainedExampleGeneric[U]] interface {
    GetString() string
    GetExplainedExampleGeneric() V
    *T // non-interface type constraint element
}

type ExplainedExampleGeneric[T any] interface {
    GetBool() bool
    *T // non-interface type constraint element
}

type UncharteredImpl struct{ some string }

func (e *UncharteredImpl) GetExplainedExampleGeneric() ExplainedImpl { return ExplainedImpl{} }
func (e *UncharteredImpl) GetString() string {
    if e == nil {
        return ""
    }
    return e.some
}

type ExplainedImpl struct{ bully bool }

func (e *ExplainedImpl) GetBool() bool { return e == nil || e.bully }

func Print[T any, PT ExplainedExampleGeneric[T]](impl T) {
    fmt.Println(PT(&impl).GetBool())
}

func UncharteredPrint[T any, U any, PT UncharteredGeneric[T, U]](impl T) { -> got 2 arguments but 3 type parameters
    fmt.Println(PT(&impl).GetExplainedExampleGeneric().GetBool()) -> cannot convert &impl (value of type *T) to type PT
}

func main() {
    Print[ExplainedImpl](ExplainedImpl{})
    UncharteredPrint[UncharteredImpl, ExplainedImpl](UncharteredImpl{}) -> cannot infer PT (prog.go:36:37)
}

知道我做错了什么吗? 似乎应该可以,但我不确定我在这里遗漏了什么,任何帮助将不胜感激。

首先,接口 UncharteredGeneric 是参数化的 类型 ,您必须显式提供所有三种类型参数。

因此在UncharteredPrint函数类型参数列表中,PT应该是:

PT UncharteredGeneric[T, U, V]

现在 V 是什么?正如在 UncharteredGeneric 自己的类型参数列表中看到的那样,V 必须满足 ExplainedExampleGeneric[U],因此这就是我们定义它的方式。完整签名变为:

UncharteredPrint[T any, U any, V ExplainedExampleGeneric[U], PT UncharteredGeneric[T, U, V]](impl T)

不能直接使用ExplainedExampleGeneric[U]来实例化UncharteredGeneric,因为ExplainedExampleGeneric包含类型元素*T;您只能将其用作 V.

的约束

最后,毕竟这个 PT 会被推断为 *UncharteredImpl,但这还没有实现 UncharteredGenericV 是用 [=29= 实例化的],但该方法声明为 GetExplainedExampleGeneric() ExplainedImpl。您还必须将方法签名固定为 return 指针类型:

func (e *UncharteredImpl) GetExplainedExampleGeneric() *ExplainedImpl { return &ExplainedImpl{} }

然后编译运行:https://go.dev/play/p/zCZd53ys-5J