func dummyGeneric[E any](input, input2 E) E {
return input
}
func dummyString(input, input2 string) string {
return input
}
func _() {
x := dummyGeneric("") // missing input 2
y := dummyString("") // missing input 2
x = "" // type of x is invalid
y = "" // type of y is string
}
In the code snippet above, both the function call miss one input parameter. The type checker successfully figured out the type of y is string because it is not a generic function. However, the type checker did not successfully figured out the type of x is string.
When developing gopls, we encounter a lot of cases where the go source file is broken especially when the user is typing. A good use case is auto-completion in a generic function call
var x []string
slices.ContainsFunc(x, ${cursor})
In snippet above, the cursor is in the position of the second input parameter. It is clear that the user want to type func(s string) bool {}. But because we can not infer the generic type, we can not offer such auto-completion.
This is a FR to ask if type checker could infer the generic type if the context provide an un-ambiguous resolution. The type checker should keep the generic type invalid if the context is confusing
func foo[E any](input1, input2, input3 E) {}
func _() {
foo("", "", ${type}) // the type is string
foo("", 1, ${type}) // the type is invalid
}
We could try to match the argument to the signature by it's order. But I'm proposing the matching mechanism simply because it is the simplest and it matches the most with the human behavior. (as we tend to write input parameter from left to right.)
Signature (arg0, arg1, arg2, arg3 ....)
/ / /
Call (input0, input1, input2 ...)
Signature (arg0, arg1, arg2, arg3 ....)
/ / /
Call (input0, ${miss}, input2 ...)
cc @adonovan @mrkfrmn
In the code snippet above, both the function call miss one input parameter. The type checker successfully figured out the type of
yis string because it is not a generic function. However, the type checker did not successfully figured out the type ofxisstring.When developing gopls, we encounter a lot of cases where the go source file is broken especially when the user is typing. A good use case is auto-completion in a generic function call
In snippet above, the cursor is in the position of the second input parameter. It is clear that the user want to type
func(s string) bool {}. But because we can not infer the generic type, we can not offer such auto-completion.This is a FR to ask if type checker could infer the generic type if the context provide an un-ambiguous resolution. The type checker should keep the generic type invalid if the context is confusing
We could try to match the argument to the signature by it's order. But I'm proposing the matching mechanism simply because it is the simplest and it matches the most with the human behavior. (as we tend to write input parameter from left to right.)
cc @adonovan @mrkfrmn