英文:
golang: trying to use `any` to match different functions
问题
给定这个函数:
func Apply(label string, func(msg any, arg any) (any, error) {
...
}
不幸的是,我需要传递给Apply
的func
在其签名上不匹配。
//Func 1
func GetAddress(prompt string, custom Param) (string, error) {}
// Func 2
func GetIdentifier(prompt string) (utils.ID, error) {}
但是,如果我尝试这样做,编译器(go1.18)会报错:
Apply(label, GetAddress)
错误信息为:
[compiler IncompatibleAssign] [E] cannot use GetAddress (value of type func(prompt string, custom Param) (string, error)) as func(msg any, arg any) (any, error) value in argument to Apply
为什么msg any
不能匹配到prompt string
,custom Param
不能匹配到arg any
,以及返回的string
类型不能匹配到返回的any
类型?
到底是什么地方不兼容?(如果你认为设计有问题,请忽略,我只是想先理解这个问题)。
英文:
Given this function:
func Apply(label string, func(msg any, arg any) (any, error) {
...
}
Unfortunately the func
s I need to pass to Apply
don't match in their signature.
//Func 1
func GetAddress(prompt string, custom Param) (string, error) {}
// Func 2
func GetIdentifier(prompt string) (utils.ID, error) {}
But why does the compiler (go1.18) complain if I try
Apply(label, GetAddress)
with
> [compiler IncompatibleAssign] [E] cannot use GetAddress (value of
> type func(prompt string, custom Param) (string, error)) as
> func(msg any, arg any) (any, error) value in argument to Apply
Why does msg any
not match to prompt string
, custom Param
not to arg any
and the string
type in the return not to the any
type in the return?
What exactly is incompatible here? (Ignore if you think the design is bad for a sec, I just want to understand that first).
答案1
得分: 1
将函数声明更改为使用类型参数
func Apply[T1 any, T2 any, T3 any](label string, func(msg T1, arg T2) (T3, error)
现在调用它
Apply[string, Param, string](label, GetAddress)
当函数具有类型参数时,你可以省略类型参数列表,编译器会自动推断变体类型是什么:
Apply(label, GetAddress)
英文:
Change the func declaration to use type parameters
func Apply[T1 any, T2 any, T3 any](label string, func(msg T1, arg T2) (T3, error)
Now to call it
Apply[string, Param, string](label, GetAddress)
When the function has type parameters, you may be able to drop the type parameter list with the compiler automatically inferring what the variant types are:
Apply(label, GetAddress)
答案2
得分: 1
你写的代码实际上无法正常工作的原因是在Go语言中,类型必须完全匹配。接口类型对于传入的值类型是有效的,但是在函数中,定义的类型必须相同。然而,如果定义的类型是接口(或者是"any"),参数值可以是任意类型(例如string|int...)。
func Apply(label string, func(msg any, arg any) (any, error) {
...
}
//Func 1
func GetAddress(prompt any, custom any) (any, error) {}
// Func 2
func GetIdentifier(prompt any) (any, error) {}
请注意,以上是你要翻译的内容。
英文:
The reason why the code you wrote doesn't actually work is that the types have to match exactly in the go language. The interface type is valid for incoming value types, the definition types must be the same in function, however, if the definition type is interface (or "any"), the parameter value can be any. (string|int ...)
func Apply(label string, func(msg any, arg any) (any, error) {
...
}
//Func 1
func GetAddress(prompt any, custom any) (any, error) {}
// Func 2
func GetIdentifier(prompt any) (any, error) {}
答案3
得分: -1
应用[K any]函数(label string, func(msg K, arg K) (K, error) {
...
}
也许像这样的代码可以工作
Apply[K any]
英文:
func Apply[K any](label string, func(msg K, arg K) (K, error) {
...
}
maybe something like this could work
Apply[K any]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论