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

gazerro opened this issue Dec 15, 2021 · 4 comments


Copy link

@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.

Copy link

@ianlancetaylor ianlancetaylor commented Dec 16, 2021

@ianlancetaylor ianlancetaylor added the NeedsInvestigation label Dec 16, 2021
@ianlancetaylor ianlancetaylor added this to the Go1.18 milestone Dec 16, 2021
Copy link

@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.

@griesemer griesemer self-assigned this Dec 16, 2021
@griesemer griesemer removed the NeedsInvestigation label Dec 16, 2021
Copy link
Contributor Author

@gazerro gazerro commented Dec 16, 2021

@gazerro Are you looking at


Copy link

@gopherbot gopherbot commented Feb 9, 2022

Change mentions this issue: spec: the type of a constant cannot be a type parameter

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet

No branches or pull requests

4 participants