问题内容
我是 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)时,使用以下内容:
立即学习“go语言免费学习笔记(深入)”;
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://www.php.cn/link/63e8e3643e7f7198858eef325b0600f9
- 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://www.php.cn/link/63e8e3643e7f7198858eef325b0600f9
- ./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}
登录后复制
但同样,我认为泛型可能不是适合您特定用例的最佳解决方案。
以上就是Golang 泛型 – 在传递函数指针时不能使用类型代替相同类型的详细内容,更多请关注【创想鸟】其它相关文章!