Open
Description
As of current tip (fb6f38d), this code works:
package main
func main() {
g(1, mkf)
}
func g[T any](x T, f func() func(T)) {}
func mkf[T any]() func(T) {
return func(x T) {}
}
but this code fails, because cannot infer the type of mkf
when it's been called:
package main
func main() {
g(1, mkf())
}
func g[T any](x T, f func(T)) {}
func mkf[T any]() func(T) {
return func(x T) {}
}
It's entirely possible that getting this to work might increase the complexity of the spec too much, but I'm raising this issue to track the possibility, because it's not an uncommon pattern. In particular, the functions used in the "functional options" pattern often return a closure that operates on the receiving type, but the lack of inference forces the caller to explicitly pass the type parameter in such cases. Here's a small example of that pattern: https://go.dev/play/p/kJJWceSqlNM?v=gotip
Metadata
Metadata
Assignees
Labels
Type
Projects
Status
Incoming