Skip to content
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

spec: constraint type inference for struct fields #57270

Closed
iangudger opened this issue Dec 12, 2022 · 1 comment
Closed

spec: constraint type inference for struct fields #57270

iangudger opened this issue Dec 12, 2022 · 1 comment
Labels
FrozenDueToAge generics Issue is related to generics NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. TypeInference Issue is related to generic type inference
Milestone

Comments

@iangudger
Copy link
Contributor

What version of Go are you using (go version)?

$ go version
go version go1.19.4 linux/amd64

Does this issue reproduce with the latest release?

Yes

What did you do?

type Element[T any] interface {
	*T
}

type Entry[T any, U Element[T]] struct{}

type testEntry struct {
	Entry[testEntry]
}

https://go.dev/play/p/RbzMxIySi3f

What did you expect to see?

Compiles.

What did you see instead?

./prog.go:10:2: got 1 arguments but 2 type parameters

If we modify the code above to the following, it compiles:

type Element[T any] interface {
	*T
}

type Entry[T any, U Element[T]] struct{}

type testEntry struct {
	Entry[testEntry, *testEntry]
}

https://go.dev/play/p/taePA8Kizz1

@ianlancetaylor ianlancetaylor changed the title types2: constraint type inference for struct fields spec: constraint type inference for struct fields Dec 13, 2022
@ianlancetaylor ianlancetaylor added NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. generics Issue is related to generics TypeInference Issue is related to generic type inference labels Dec 13, 2022
@ianlancetaylor ianlancetaylor added this to the Backlog milestone Dec 13, 2022
@griesemer
Copy link
Contributor

The issue here is not missing inference for struct fields but missing type inference for generic types (Entry is a generic type). If Entry is a generic function, type inference does work (adjusted example).

We don't do type inference for types because it is significantly more complex than type inference for functions: generic types may refer to themselves recursively in ways that may influence type inference. We don't have a good understanding what that means.

We are not planning to do this any time soon, if at all. In any case, it would require a thorough proposal. Closing this issue.

@golang golang locked and limited conversation to collaborators Mar 7, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge generics Issue is related to generics NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. TypeInference Issue is related to generic type inference
Projects
None yet
Development

No branches or pull requests

4 participants