Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

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

Comments

Projects
None yet
6 participants
Contributor

griesemer commented Nov 3, 2016

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

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

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.

Contributor

alandonovan commented Nov 4, 2016

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.

Contributor

griesemer commented Nov 4, 2016

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

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.

@gopherbot gopherbot closed this in 87f4e36 Nov 4, 2016

Contributor

rsc commented Nov 27, 2016

Locking because aliases are no longer proposed.

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