digitalmars.dip.ideas - continue in labeled switch
- monkyyy (19/19) Jul 04 from the 2026 update for zig he showed off labeled switches
- Nick Treleaven (18/37) Jul 05 Why is that better than using `goto case 1;`, `goto case 2;`?
- Nick Treleaven (3/9) Jul 05 Sorry, wrong link:
- user1234 (3/4) Jul 05 The example of OP is not good, the point is that you can jump
- monkyyy (13/52) Jul 05 ```d
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 someone tell walter zig is better c because of this
Jul 04
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 gotoWhy 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.
Jul 05
On Saturday, 5 July 2025 at 10:18:28 UTC, Nick Treleaven wrote: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(!):Sorry, wrong link: https://ziglang.org/documentation/master/#Labeled-switchSemantically, this is equivalent to the following loop:https://ziglang.org/documentation/master/#toc-Switching-with-Enum-Literals
Jul 05
On Saturday, 5 July 2025 at 10:18:28 UTC, Nick Treleaven wrote:Why is that better than using `goto case 1;`, `goto case 2;`?The example of OP is not good, the point is that you can jump from a nested switch to its parent switch.
Jul 05
On Saturday, 5 July 2025 at 10:18:28 UTC, Nick Treleaven wrote:On Saturday, 5 July 2025 at 02:27:33 UTC, monkyyy wrote:```d void collez(int i){ final switch(i%2){ case 0: i/=2; goto case (i%2); //error case 1: i*=3;i++; goto case (i%2); }} ``` reusing the switch machineryfrom 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 gotoWhy 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.
Jul 05