-
Notifications
You must be signed in to change notification settings - Fork 17.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
proposal: spec: apply constraint type inference to generic types #62460
Comments
There's an awful lot of what looks, to me at least, like extraneous information in those code examples that are kind of obscuring the actual change. The diff between the two is just @@ -6,7 +6,7 @@
func main() {
c := New[testType]()
- c.Txn(func(txn Txn[testType, *testType]) {
+ c.Txn(func(txn Txn[testType]) {
txn.New(testType{val: "1"})
txn.Update("0", func(tt testType) (out testType) {
return |
And as far as I can tell that's only possible in the specific case |
It seems this issue is about redundant parameter type declaration in closures. |
I'm not sure about that. I think it's actually related to a specific case of structural constraints. |
Can you describe the new type inference that you are looking for using the kind of language as issue #58650? Can you provide a minimal example? Thanks. |
You're 100% correct. Sorry about that! Thank you for clarifying the changes |
The "Constraint type inference 2" example already works, though, so there is some essential difference in this proposal. |
I think the essential difference - based on the diff - is that this proposal is about applying constraint type inference to generic types, not just generic function calls. That is, the goal is to make this work: package main
func main() {
var x X[*S]
_ = x
}
type C[T any] interface {
*T
M()
}
type X[PT C[T], T any] struct {
v T
}
func (x *X[PT, T]) N() {
PT(&x.v).M()
}
type S struct{}
func (*S) M() {} This proposal specifically asks about a situation where the type appears as the argument type in a function-literal passed to a function call. But I think the main hurdle is still, that we don't even try to infer type-arguments for generic types. |
@Merovius you make some fantastic points and you are 💯 correct. I appreciate you noticing the subtle differences that I missed. |
CC @griesemer |
@itsmontoya We're not doing any inference (specifically "constraint type inference") when instantiating generic types for now because of issues with type cycles that we don't fully understand: in contrast to generic functions, a generic type may occur in its own type parameter list (e.g., as constraint or part of a constraint), directly or indirectly. The resulting cycles lead to difficult problems. We probably could detect such (for now) invalid cycles, but that also requires some theory behind it; and we haven't had time to look into this. We may do this at some point but nothing is planned in the near future. Putting on hold for now. |
Author background
Related proposals
Proposal
type MyInterface[T] interface { *T }
to be inferred betterCosts
Example - Current Requirement
Example - Proposal
The text was updated successfully, but these errors were encountered: