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

cmd/go2go: can't call function with generic type that implements generic interface #40018

Open
benjaminjkraft opened this issue Jul 3, 2020 · 1 comment

Comments

@benjaminjkraft
Copy link

@benjaminjkraft benjaminjkraft commented Jul 3, 2020

Consider the following (playground):

package main

type I(type T) interface {
	M() T
}

type S(type T) struct{}

func (s S(T)) M() T {
	var zero T
	return zero
}

func TakesI(type T)(i I(T)) {}

func main() {
	var s S(int)
	var i I(int)
	i = s          // ok
	TakesI(i)      // ok
	TakesI(int)(s) // ok
	TakesI(s) // type S(int) of s does not match I(T)
}

In short, we have a generic interface I(T), a generic type S(T) which implements it, and a function which accepts an I(T). Given a value of type S(int), we can assign it to I(int), but we can only pass the function if we explicitly pass the type parameter. If we don't, we get an error type S(int) of s does not match I(T). Offhand, I would think the compiler should be able to infer just fine here, but if not it should just say cannot infer T.

@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Jul 3, 2020

CC @griesemer

I agree that in this case the compiler can in principle infer the type argument. But that is not part of the type inference rules described in the design draft. I think this is related to the discussion at https://go.googlesource.com/proposal/+/refs/heads/master/design/go2draft-type-parameters.md#type-inference-for-generic-function-arguments .

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
2 participants
You can’t perform that action at this time.