On Sun, Oct 7, 2012 at 7:09 PM, Dan Kortschak
<
dan.ko...@adelaide.edu.au> wrote:
> Can someone explain why this is illegal (./cluster.go:40: invalid
> continue label ATTEMPT)? Should I just be using goto here? My use here
> feels sane, and continue indicates what I mean, but it seems that the
> label must immediately precede the for loop - is this the case? It seems
> restrictive.
Yes, this is the case. I don't know that it is restrictive, it's just
trying not to be error-prone. Continue is an operation on for loops.
When you say continue, it refers to the innermost for loop. If you
want to specify a different loop to continue, you have to label the
loop, not a statement inside the loop. Requiring that the continue
label actually label a loop avoids errors guessing what loop is meant
when the label points elsewhere. For what it's worth, I believe this
is the same behavior as in Java. This has not changed in many years.
The revision d4f8b77723db that you found was fixing a corner case
where in
L:{}
for x := range y {
break L
}
the L label was still treated (incorrectly) as labeling the for loop.
But a label inside a loop body has never been considered to label the
loop holding that body, and that revision predates the public release.
The label behavior has not changed since the public release.
If we allowed the label to go either inside the loop body or before
the loop, as in
for x := range y {
L:
f()
for z := range w {
continue L
}
}
then if we delete the call to f, the label becomes ambiguous.
Russ