尝试使用`any`来匹配不同的函数。

huangapple go评论92阅读模式
英文:

golang: trying to use `any` to match different functions

问题

给定这个函数:

func Apply(label string, func(msg any, arg any) (any, error) {
...
}

不幸的是,我需要传递给Applyfunc在其签名上不匹配。

//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 stringcustom Param不能匹配到arg any,以及返回的string类型不能匹配到返回的any类型?

到底是什么地方不兼容?(如果你认为设计有问题,请忽略,我只是想先理解这个问题)。

英文:

Given this function:

func Apply(label string, func(msg any, arg any) (any, error) {
...
}

Unfortunately the funcs 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]

huangapple
  • 本文由 发表于 2022年8月17日 07:02:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/73381207.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定