我是 golang 新手,我正在研究以下结构:
type Flag[T any] struct {
defaultValue interface{}
}
其中
T
可以是 int
或 bool
我定义了以下函数:
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
}
但是,我认为泛型可能不是适合您特定用例的最佳解决方案。