You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
./prog.go:17:10: not enough type arguments for type Impl: have 1, want 2
./prog.go:18:11: not enough type arguments for type Impl: have 1, want 2
What did you expect to see?
I expected the second type parameter (T) to be able to be inferred for two reasons:
First reason: Method1 uses the T generic parameter in its signature. If an incorrect second type parameter is supplied the compiler complains about it, so it knows about this relation between the method signature and type parameter. For example, var tmp4 Impl[Thingy[int], string] results in:
./prog.go:21:16: Thingy[int] does not satisfy IFace1[string] (wrong type for method Method1)
have Method1(int) bool
want Method1(string) bool
Looking at the above example it appears like the user supplied second type parameter takes precedence over any value supplied in the first type parameter as the error expects the type that the user supplied in the second parameter. But if the second argument is not supplied, would it be safe to infer the type? If it is not an explanation as to why the type cannot be inferred or a counter example would be appreciated.
Second reason: Most surprisingly, functions do not have this limitation. The type inference works as I would expect on Func1 and Func2 in the example.
The text was updated successfully, but these errors were encountered:
Go version
go version go1.23.3 linux/amd64
Output of
go env
in your module/workspace:What did you do?
I have made the below example code:
Go playground link: https://go.dev/play/p/0v8JtrzGqQn
What did you see happen?
A compiler error:
What did you expect to see?
I expected the second type parameter (
T
) to be able to be inferred for two reasons:Method1
uses theT
generic parameter in its signature. If an incorrect second type parameter is supplied the compiler complains about it, so it knows about this relation between the method signature and type parameter. For example,var tmp4 Impl[Thingy[int], string]
results in:Looking at the above example it appears like the user supplied second type parameter takes precedence over any value supplied in the first type parameter as the error expects the type that the user supplied in the second parameter. But if the second argument is not supplied, would it be safe to infer the type? If it is not an explanation as to why the type cannot be inferred or a counter example would be appreciated.
Func1
andFunc2
in the example.The text was updated successfully, but these errors were encountered: