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

spec: document that type parameters are not permitted as constant declaration types #50202

Open
gazerro opened this issue Dec 15, 2021 · 3 comments
Open
Assignees
Milestone

Comments

@gazerro
Copy link
Contributor

@gazerro gazerro commented Dec 15, 2021

The compilation of the following program fails with error ./prog.go:4:10: invalid constant type P

package main

func f[P int]() {
	const y P = 5
}

func main() {}

but this behavior is not documented in the spec.

From the spec 5 is assignable to P because it is an untyped constant representable by a value of type P.

@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Dec 16, 2021

@griesemer
Copy link
Contributor

@griesemer griesemer commented Dec 16, 2021

I believe this is not yet fully documented in the in-progress spec. But here it is: type parameters cannot be constant types. While 5 can be stored in a variable of type P, it cannot be the value of a constant of type P. Specifically, and this is documented in the section on conversions, if a constant is converted to a type parameter, say P(5), the result is a non-constant value.

Leaving open until fully documented.

@gazerro
Copy link
Contributor Author

@gazerro gazerro commented Dec 16, 2021

@gazerro Are you looking at https://tip.golang.org/ref/spec?

Yes.

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
4 participants