continue in labeled switch
Nick Treleaven
nick at geany.org
Sat Jul 5 10:18:28 UTC 2025
On Saturday, 5 July 2025 at 02:27:33 UTC, monkyyy wrote:
> from the 2026 update for zig he showed off labeled switches
> https://youtu.be/x3hOiOcbgeA?si=rr69iKJOZ2CyKysi
>
> ```d
> foo: switch(0){
> case 0:
> "hi".writeln;
> continue foo(2);
> case 1:
> "bye".writeln;
> break;
> case 2:
> "hello".writeln;
> continue foo(1);
> default: assert(0);
> }
> ```
> would print "hi","hello","bye" this is probably the majority of
> my uses of goto
Why is that better than using `goto case 1;`, `goto case 2;`?
https://dlang.org/spec/statement.html#goto-statement
Using `continue` for this seems like unnecessary keyword meaning
overloading, given that AIUI `continue;` still refers to loop
statement iteration, and a labelled switch lowers to a while
loop(!):
> Semantically, this is equivalent to the following loop:
https://ziglang.org/documentation/master/#toc-Switching-with-Enum-Literals
So it seems using `continue;` would silently restart the labelled
switch statement, probably causing an infinite loop (if the
implementation matches the spec). Or probably that is/will be a
compile error, in which case labelled switch has limitations
versus unlabelled switch.
Also is there a `continue foo(default);`? Having `goto default;`
is better than having to write e.g. `continue foo(3);`, because
`case 3:` could be added later, and jumping to the default case
may have been the intended target - updating the latter code can
easily be forgotten, especially for a long switch statement.
More information about the dip.ideas
mailing list