Break statements in labelled switches, footgun?

But that still doesn’t explain what the problem with switch-as-loop itself is. The problem in the OP with break not targeting the intended block appears to be mostly orthogonal from switch statements as a looping construct. Even before labeled switches, you could already run into similar problems if you had an outer loop containing a nested labeled block:

for (values) |x| {
    blk: {
        if (x == 100) break :blk;
        doSomething(x);
        break; // oops, we forgot the label
    }
    doSomeOtherThing(x);
}

I don’t think that means that labeled blocks should be removed. Perhaps it would be useful if it was a compile error to use unlabeled continues or breaks in situations where there could be multiple intended targets and the author’s intent is ambiguous?

If what you mean by this is “Zig switches should not loop because switches in C and C-influenced languages don’t loop”, then that seems like a good example of a local maximum, which is something Zig deliberately strives to avoid.

4 Likes