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: Generic constraint without specified type in function #40976

Closed
zweihander opened this issue Aug 22, 2020 · 1 comment
Closed

cmd/go2go: Generic constraint without specified type in function #40976

zweihander opened this issue Aug 22, 2020 · 1 comment

Comments

@zweihander
Copy link

@zweihander zweihander commented Aug 22, 2020

go2go playground:

package main

import "fmt"

type SomeConstraint[T] interface{
	Foo() T
}

func Bar[T SomeConstraint](val T) { // [T SomeConstraint[int]] works well
	fmt.Println(val.Foo())
}

type impl struct{}
func (impl) Foo() int { return 1337 }

func main() {
	Bar(impl{})
}

Got the error: impl does not satisfy SomeConstraint(T) (missing method Foo).
How to solve it? Or, if problem is in the function declaration, we need a better error message.

@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Aug 22, 2020

When you write just SomeConstraint as the constraint in Bar, it means SomeConstraint[T]. That means that any type argument A must have a method Foo() A. Your type impl does not have such a method. It has Foo() int, but it needs to have Foo() impl.

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.