Open logandavies181 opened 1 year ago
This is expected with the current language spec. There is no type inference rule that permits inferring the type argument for T
in funcT[T]
.
Putting on hold because we also don't support calling the function in the generic code.
I also ran into this earlier and would like to see this sort of inference be possible. You can absolutely call the generic function.
package main
import "fmt"
type funcT[T any] interface {
func(T) | func(T) error
}
func foo[T any, F funcT[T]](f F) {
switch any(f).(type) {
case func(T) error:
any(f).(func(T) error)(*new(T))
case func(T):
any(f).(func(T))(*new(T))
}
}
func bar(i int) {
fmt.Println("bar called with: ", i)
}
func bar2(i int) error {
fmt.Println("bar2 called with: ", i)
return nil
}
func main() {
foo[int](bar) // cannot infer here
foo[int](bar2) // or here
}
Output:
bar called with: 0
bar2 called with: 0
Edit: Removed other example.
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
Try to use type inference for a generic interface whose only member is a union of functions
https://go.dev/play/p/uPJQhqqwMk0
What did you expect to see?
Program compiled successfully
What did you see instead?
./prog.go:12:5: cannot infer T (prog.go:7:10)
If the interface doesn't have a union then type inference works as expected:
https://go.dev/play/p/B-yRqxfvaK0