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
shadowspore opened this issue Aug 22, 2020 · 1 comment
Closed

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

shadowspore opened this issue Aug 22, 2020 · 1 comment

Comments

@shadowspore
Copy link

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

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.

@golang golang locked and limited conversation to collaborators Aug 22, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants