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: Cannot return T from method receiving Generic[T] #57182

Closed
fschuetz04 opened this issue Dec 8, 2022 · 5 comments
Closed

go/types: Cannot return T from method receiving Generic[T] #57182

fschuetz04 opened this issue Dec 8, 2022 · 5 comments

Comments

@fschuetz04
Copy link

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

Go Playground with Go 1.18, 1.19, and dev branch.

Does this issue reproduce with the latest release?

Yes.

What did you do?

See https://go.dev/play/p/fCO1GDA_oZj?v=gotip for an example.

I defined a generic type, e. g.:

type Generic[T any] struct{}

Then I wanted to add a method receiving an instantiation of this generic type and returning T, e. g.:

func (_ Generic[uint8]) Value() uint8 {
	return 0
}

What did you expect to see?

I would expect the program to build without errors.

What did you see instead?

Adding the method leads to an error when building the program:

cannot use 0 (untyped int constant) as uint8 value in return statement

The program only builds if the return type of the method is different from T.

@seankhliao
Copy link
Member

this doesn't do what you think it does.
methods can't be on specific instantiations.

Unlike many projects, the Go project does not use GitHub Issues for general discussion or asking questions. GitHub Issues are used for tracking bugs and proposals only.

For questions please refer to https://github.com/golang/go/wiki/Questions

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Dec 9, 2022
@timothy-king
Copy link
Contributor

func (_ Generic[uint8]) Value() uint8 { return 0 } does not mean what you think it means. This is the same as func (_ Generic[T]) Value() T { return 0 }. You are overloading the name uint8 with a type parameter named "uint8" that is constrained to be an any. This is the meaning of the "uint8" return value. Hence the very confusing error message.

@timothy-king
Copy link
Contributor

I vaguely recall there was a suggested vet check for this.

@seankhliao
Copy link
Member

the check was deemed premature at the time #48123

@ianlancetaylor
Copy link
Contributor

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

5 participants