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: clarify meaning of alias decls in sequence of "enum" const declaration #17784

Closed
griesemer opened this issue Nov 3, 2016 · 6 comments
Closed
Milestone

Comments

@griesemer
Copy link
Contributor

package main

import "math"
import "fmt"

const (
	a = iota
	b
	c => math.Pi
	d
)

func main() {
	fmt.Println(a, b, c, d)
}

prints

0 1 3.141592653589793 2

It probably shouldn't be allowed in the first place. Certainly the output is wrong (iota should be incremented with each semicolon).

@griesemer griesemer self-assigned this Nov 3, 2016
@griesemer griesemer added this to the Go1.8 milestone Nov 3, 2016
@gopherbot
Copy link

CL https://golang.org/cl/32597 mentions this issue.

@mdempsky
Copy link
Member

mdempsky commented Nov 3, 2016

Certainly the output is wrong (iota should be incremented with each semicolon).

Currently the spec says iota's value increments after each ConstSpec, and AliasSpec is disjoint from ConstSpec. So I think replacing "d" with "d = iota" would logically assign 2 to d, under the current wording.

That said, if we did want to change it to increment at semicolons, I'm not opposed to that. I just expect in practice it won't really matter. If we choose to increment only after ConstSpecs, users can still include blank ConstSpecs next to AliasSpecs to increment iota; conversely, they can move AliasSpecs out-of-line to prevent them from incrementing iota.

@alandonovan
Copy link
Contributor

I agree with Matthew that the current interpretation is clear, and that it matters little in practice. I also think the current interpretation is defensible: iota shouldn't increment after an alias declaration that can't possibly use it.

@griesemer
Copy link
Contributor Author

@mdempsky, @alandonovan Good point - with the current spec, incrementing iota with each ConstSpec is the same as incrementing with each semicolon...

There's a simpler solution: Do not alias const aliases in the first place. Just something to keep in mind.

@jimmyfrasche
Copy link
Member

If you replace => with = it prints

0 1 3.141592653589793 3.141592653589793 ( https://play.golang.org/p/4xOWo5vx8P )

Since => and = are essentially the same for consts, I'd expect them to behave identically.

@rsc
Copy link
Contributor

rsc commented Nov 27, 2016

Locking because aliases are no longer proposed.

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

No branches or pull requests

6 participants