問題內(nèi)容
我是 golang 新手,我正在研究以下結(jié)構(gòu):
type Flag[T any] struct {
defaultValue interface{}
}
登錄后復(fù)制
其中 T
可以是 int
或 bool
我定義了以下函數(shù):
func (f Flag[T]) GetVariation(val interface{}, getFunc func(v T) T ) T {
inputVal := f.defaultValue.(T)
return getFunc(inputVal)
}
登錄后復(fù)制
當(dāng)我嘗試將上述函數(shù)用于各種數(shù)據(jù)類型(例如 bool)時(shí),使用以下內(nèi)容:
func (f Flag[bool]) GetBoolVariation(val bool) bool {
return f.GetVariation(val, someFunc)
}
func someFunc(v bool) bool {
return true
}
登錄后復(fù)制
我收到以下錯(cuò)誤消息:
cannot use someFunc (value of type func(v bool) bool) as func(v bool) bool value in argument to f.GetVariation
登錄后復(fù)制
該消息非常令人困惑,因?yàn)樗f我不能將“X”用作“X”。你能幫我弄清楚我在這里做錯(cuò)了什么嗎?
正確答案
首先,很難在這里衡量您的更大用例,但泛型可能不是最適合這里,因?yàn)槟M(jìn)行運(yùn)行時(shí)類型檢查(例如 f.defaultValue.(T)
)。
其次,您似乎正在使用 go 1.20
,這確實(shí)會(huì)產(chǎn)生一個(gè)令人困惑的錯(cuò)誤:
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
登錄后復(fù)制
使用最新的 Playground 版本(截至撰寫本文時(shí)為 go 1.21
)會(huì)給出更詳細(xì)的編譯錯(cuò)誤:
https://www.php.cn/link/2d1bcedd27b586d2a9562a0f8e076b41
./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
登錄后復(fù)制
指示類型 bool
被指定的位置 (./prog.go:13:14
) 源于類型約束。
因此,僅僅因?yàn)榇祟愋图s束 bool
與非泛型函數(shù)簽名匹配:
func someFunc(v bool) bool { return true }
登錄后復(fù)制
并不意味著它是精確的編譯匹配。
您可以使用這個(gè)人為的示例“解決”編譯錯(cuò)誤:
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 }
登錄后復(fù)制
但同樣,我認(rèn)為泛型可能不是適合您特定用例的最佳解決方案。