首页 > 文章列表 > Golang 泛型 - 遇到函数指针时不能使用类型参数替代相同类型

Golang 泛型 - 遇到函数指针时不能使用类型参数替代相同类型

139 2024-03-04
问题内容

我是 golang 新手,我正在研究以下结构:

type Flag[T any] struct {
    defaultValue interface{}
}

其中 T 可以是 intbool

我定义了以下函数:

func (f Flag[T]) GetVariation(val interface{}, getFunc func(v T) T ) T {
    inputVal := f.defaultValue.(T)

    return getFunc(inputVal)
}

当我尝试将上述函数用于各种数据类型(例如 bool)时,使用以下内容:

func (f Flag[bool]) GetBoolVariation(val bool) bool {
    return f.GetVariation(val, someFunc)
}

func someFunc(v bool) bool {
    return true
}

我收到以下错误消息:

cannot use someFunc (value of type func(v bool)  bool) as func(v bool) bool value in argument to f.GetVariation

该消息非常令人困惑,因为它说我不能将“X”用作“X”。你能帮我弄清楚我在这里做错了什么吗?


正确答案


首先,很难在这里衡量您的更大用例,但泛型可能不是最适合这里,因为您要进行运行时类型检查(例如 f.defaultValue.(T))。

其次,您似乎正在使用 go 1.20,这确实会产生一个令人困惑的错误:

https://go.dev/play/p/IpykGv6yxZt?v=goprev

cannot use someFunc (value of type func(v bool) bool) as func(v bool) bool value in argument to f.GetVariation

使用最新的 Playground 版本(截至撰写本文时为 go 1.21)会给出更详细的编译错误:

https://go.dev/play/p/IpykGv6yxZt

./prog.go:14:29: cannot use someFunc (value of type func(v bool) bool) as func(v bool /* with bool declared at ./prog.go:13:14 */) bool /* with bool declared at ./prog.go:13:14 */ value in argument to f.GetVariation

指示类型 bool 被指定的位置 (./prog.go:13:14) 源于类型约束。

因此,仅仅因为此类型约束 bool 与非泛型函数签名匹配:

func someFunc(v bool) bool {
    return true
}

并不意味着它是精确的编译匹配。

您可以使用这个人为的示例“解决”编译错误:

func someFuncFix[T any](v T) T {
    return v
}

func (f Flag[bool]) GetBoolVariation(val bool) bool {
    return f.GetVariation(val, someFuncFix[bool])

    // FYI: `return f.GetVariation(val, someFuncFix)` also works as the bool constraint could be inferred
}

但同样,我认为泛型可能不是适合您特定用例的最佳解决方案。