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: scope and re-use of loop variables is not fully specified #7834

Closed
dominikh opened this issue Apr 22, 2014 · 5 comments
Closed

spec: scope and re-use of loop variables is not fully specified #7834

dominikh opened this issue Apr 22, 2014 · 5 comments
Assignees
Milestone

Comments

@dominikh
Copy link
Member

@dominikh dominikh commented Apr 22, 2014

For "for" statements, the spec only partially covers the scope and reuse of
variables declared with a short variable declaration, as well as for the use of existing
variables.

When using the `range` keyword, the spec says that 

„The iteration variables may be declared by the "range" clause using a form
of short variable declaration (:=). [...] and their scope ends at the end of the
"for" statement; they are re-used in each iteration. If the iteration
variables are declared outside the "for" statement, after execution their
values will be those of the last iteration“

All of these statements are also true for "for" statements that use a
ForClause, that is `for i := 0; i < N; i++`, but the spec does not explicitly mention
this. 

The section on "Short variable declarations" does say that 

„In some contexts such as the initializers for "if", "for", or
"switch" statements, they can be used to declare local temporary variables“

but "local temporary variables" is vague and only covers the fact that their
scope ends at the end of the "for" statement; it does not say anything about
the variables' re-use. And obviously it doesn't cover the use of variables that are
declared outside the "for" statement.

I propose to split the first quoted paragraph into two: One that's general to the
"for" statement itself, and one that only covers the bits that are specific to
the use of "range".
@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Apr 22, 2014

Comment 1:

Labels changed: added repo-main, release-go1.4, documentation.

Owner changed to @griesemer.

@griesemer

This comment has been minimized.

Copy link
Contributor

@griesemer griesemer commented Apr 22, 2014

Comment 2:

Status changed to Accepted.

@griesemer

This comment has been minimized.

Copy link
Contributor

@griesemer griesemer commented Sep 24, 2014

Comment 3:

I opted for repeating some of the wording for clarity. Note that the variables declared
in a regular for loop's init statement (w/o a range clause) are not necessarily
"iteration variables".
https://golang.org/cl/148940044

Status changed to Started.

@gopherbot

This comment has been minimized.

Copy link

@gopherbot gopherbot commented Sep 24, 2014

Comment 4:

CL https://golang.org/cl/148940044 mentions this issue.
@griesemer

This comment has been minimized.

Copy link
Contributor

@griesemer griesemer commented Sep 25, 2014

Comment 5:

This issue was closed by revision 2fa3e43.

Status changed to Fixed.

@rsc rsc added this to the Go1.4 milestone Apr 14, 2015
@rsc rsc removed the release-go1.4 label Apr 14, 2015
@golang golang locked and limited conversation to collaborators Jun 25, 2016
wheatman added a commit to wheatman/go-akaros that referenced this issue Jun 25, 2018
Fixes golang#7834.

LGTM=iant, rsc, r
R=r, rsc, iant, ken
CC=golang-codereviews
https://golang.org/cl/148940044
wheatman added a commit to wheatman/go-akaros that referenced this issue Jun 26, 2018
Fixes golang#7834.

LGTM=iant, rsc, r
R=r, rsc, iant, ken
CC=golang-codereviews
https://golang.org/cl/148940044
wheatman added a commit to wheatman/go-akaros that referenced this issue Jul 9, 2018
Fixes golang#7834.

LGTM=iant, rsc, r
R=r, rsc, iant, ken
CC=golang-codereviews
https://golang.org/cl/148940044
wheatman added a commit to wheatman/go-akaros that referenced this issue Jul 30, 2018
Fixes golang#7834.

LGTM=iant, rsc, r
R=r, rsc, iant, ken
CC=golang-codereviews
https://golang.org/cl/148940044
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants
You can’t perform that action at this time.