不能在函数调用中使用 golang 泛型变量

Cannot use golang generics variable in function call

我想了解 go1.18 中的泛型实现。在我的测试示例中,我存储了一系列测试用例并尝试调用一个函数变量。不幸的是,当我尝试使用变量 tc.input 时,EvalCases 函数出现错误,我收到以下错误:

cannot use input (variable of type T constrained by Inputer) as type string in argument to fn

为什么我会收到该错误,我该如何解决?


import (
    "fmt"
    "strconv"
)

type BoolCase func(string) bool

type Inputer interface {
    int | float64 | ~string
}

type Wanter interface {
    Inputer | bool
}

type TestCase[T Inputer, U Wanter] struct {
    input T
    want  U
}

type TestConditions[T Inputer, U Wanter] map[string]TestCase[T, U]

// IsNumeric validates that a string is either a valid int64 or float64
func IsNumeric(s string) bool {
    _, err := strconv.ParseFloat(s, 64)
    return err == nil
}

func EvalCases[T Inputer, U Wanter](cases TestConditions[T, U], fn BoolCase) {

    for name, tc := range cases {
        input := T(tc.input)
        want := tc.want
        
        // Error: cannot use input (variable of type T constrained by Inputer) as type string in argument to fn
        got := fn(input)
        fmt.Printf("name: %-20s | input: %-10v | want: %-10v | got: %v\n", name, input, want, got)
    }
}

func main() {

    var cases = TestConditions[string, bool]{
        "empty":   {input: "", want: false},
        "integer": {input: "123", want: true},
        "float":   {input: "123.456", want: true},
    }
    fn := IsNumeric
    EvalCases(cases, fn)

}

Why am I receiving that error

因为fn是一个BoolFunc,是一个func(string) bool,所以需要一个string作为参数,而inputT。此外,根据您的定义,T 满足 Inputer 约束,因此也可以假定类型 intfloat64 或任何非 string 类型 string 作为其基础类型 (~string) none 其中隐式转换为 string.

how do I fix it?

您需要将 BoolCase 的定义更改为它自己的一个参数有一个泛型类型参数。您可以将其限制为 Inputer,但您也可以使用 any (interface{}).

type BoolCase[T any] func(T) bool

然后确保在函数签名中提供此泛型类型参数 EvalCases:

func EvalCases[T Inputer, U Wanter](cases TestConditions[T, U], fn BoolCase[T])

https://go.dev/play/p/RdjQXJ0WpDh