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

Closed
gazerro opened this issue Dec 15, 2021 · 4 comments

Comments

@gazerro
Copy link
Contributor

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

CC @griesemer

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

@ianlancetaylor ianlancetaylor added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Dec 16, 2021
@ianlancetaylor ianlancetaylor added this to the Go1.18 milestone Dec 16, 2021
@griesemer
Copy link
Contributor

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 Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Dec 16, 2021
@gazerro
Copy link
Contributor Author

gazerro commented Dec 16, 2021

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

Yes.

@gopherbot
Copy link

Change https://go.dev/cl/384240 mentions this issue: spec: the type of a constant cannot be a type parameter

@golang golang locked and limited conversation to collaborators Jun 22, 2023
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

4 participants