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

go/types, types2: make constraint type inference more flexible for some channel types (?) #45920

Open
griesemer opened this issue May 3, 2021 · 3 comments
Assignees
Milestone

Comments

@griesemer
Copy link
Contributor

@griesemer griesemer commented May 3, 2021

Constraint type inference works as expected for

package p

func Recv[T any, C interface{ type chan T }](ch C)

func _(ch chan int) {
	Recv(ch) // T is inferred as int
}

but fails for

package p

func Recv[T any, C interface{ type chan T, <-chan T }](ch C)

func _(ch chan int) {
	Recv(ch) // inference fails
}

per the current proposal specification (constraint type inference expects exactly one type in the type list/set of the constraint for which inference is used).

This seems like it might be a common case; perhaps we need to special case channels that are the same but for their directions.

Reported by @rogpeppe in #43671.

@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented May 3, 2021

I think that what is most important for now is that the inference rules be as simple as possible. Constraint type inference is already quite complicated. I don't think that we should make it more complicated. There will always be cases where a person can easily see what the answer is even if the inference algorithm can't.

@rogpeppe
Copy link
Contributor

@rogpeppe rogpeppe commented May 6, 2021

FWIW here's another case that came up while discussing possible future directions for encoding/json: https://go2goplay.golang.org/p/27Vkw9dnhNZ

It would be kinda nice to have constraint type inference work there.

@griesemer
Copy link
Contributor Author

@griesemer griesemer commented May 6, 2021

The good thing about type inference is that we can continue to fine-tune it (and make it more powerful) without affecting existing code (i.e. w/o affecting backward-compatibility).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants