proposal: infer parameter type from arguments in call to result function #64197
Labels
generics
Issue is related to generics
Proposal
TypeInference
Issue is related to generic type inference
Milestone
Suppose we have a generic function F whose result type is a function. In an expression that calls F and immediately calls the result function, it would be nice to use the arguments to the result function to help determine the type parameters for F.
For example, consider:
In Go 1.21.4, this does not compile; the error is "cannot infer X". But there is only one reasonable choice for X: *os.File. In principle, the compiler could figure this out; I don't know how difficult it would be in practice.
In this specific example, the problem can be worked around by changing the order of parameter lists:
However, I find this order much less desirable, for various reasons not relevant here. And there are undoubtedly cases where such a workaround is not possible. In particular, if both parameter lists use type parameters:
then there seems to be no way to write a call
foo(x)(y)
without explicitly specifying at least one type parameter.Like #50285 (comment), this seems to be a request to infer types from the context of a function call, although in a different manner.
There would be some open questions about how far this might go. Could we have
where
"seven"
is used to infer a type parameter of f? What if the result type is not a function, but the expression calls a method on the result? Should this work?The text was updated successfully, but these errors were encountered: