invalid continue label

986 views
Skip to first unread message

Dan Kortschak

unread,
Oct 7, 2012, 7:09:37 PM10/7/12
to golan...@googlegroups.com
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.

thanks
Dan

func ClusterFeaturePairs(p []*pals.FeaturePair, epsilon float64, effort int) (pc [][]*pals.FeaturePair, rf []*feat.Feature) {
km := kmeans.NewKmeans(Pairs(p))

values := km.Values()
cut := make([]float64, len(values))
for i, v := range values {
l := epsilon * (v.Y() - v.X())
cut[i] = l * l
}

for k := 1; k <= len(p); k++ {
ATTEMPT:
var e int
if k == 1 {
e = 1
} else {
e = effort
}
for attempt := 0; attempt < e; attempt++ {
km.Seed(k)
km.Cluster()
centers := km.Means()
for i, v := range values {
dx, dy := centers[v.Cluster()].X()-v.X(), centers[v.Cluster()].Y()-v.Y()
ok := dx*dx+dy*dy < cut[i]
if !ok {
continue ATTEMPT
}
}

pc = make([][]*pals.FeaturePair, k)
rf = make([]*feat.Feature, k)
for ci, c := range km.Clusters() {
rf[ci] = &feat.Feature{Location: p[0].A.Location, Start: int(centers[ci].X()), End: int(centers[ci].Y())}
pc[ci] = make([]*pals.FeaturePair, centers[ci].Count())
for pi, i := range c {
pc[ci][pi] = p[i]
}
}

return
}
}

panic("cannot reach")
}


David Symonds

unread,
Oct 7, 2012, 7:12:33 PM10/7/12
to Dan Kortschak, golan...@googlegroups.com

Put the label before the for loop instead of inside it.

Dan Kortschak

unread,
Oct 7, 2012, 7:16:30 PM10/7/12
to David Symonds, golan...@googlegroups.com
But that's not what I mean. It is before the relevant loop. If I replace
continue with goto I get the behaviour I intend.

David Symonds

unread,
Oct 7, 2012, 7:19:21 PM10/7/12
to Dan Kortschak, golan...@googlegroups.com

It has to be immediately before the loop. If you want it where you have it, use goto instead.

Dan Kortschak

unread,
Oct 7, 2012, 7:21:59 PM10/7/12
to David Symonds, golan...@googlegroups.com
OK. That was changed in d4f8b77723db?

Russ Cox

unread,
Oct 7, 2012, 10:46:59 PM10/7/12
to Dan Kortschak, golan...@googlegroups.com
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

Dan Kortschak

unread,
Oct 7, 2012, 11:00:31 PM10/7/12
to Russ Cox, golan...@googlegroups.com
Looking at my repository more carefully (it was around June the last
time I touched this code, so there is no reliable memory of what I was
doing) I think the situation is that I had made the change of including
the conditional block between the label and loop and then moved on to
something else without testing (the change was not committed).

Thanks for taking the time to explain.

Dan
Reply all
Reply to author
Forward
0 new messages