digitalmars.dip.ideas - static switch statement
- IchorDev (2/2) Jul 27 The equivalent of `static if`, but for `switch` statements.
- monkyyy (20/22) Jul 28 Id suggest reusing template specailization logic
- Atila Neves (3/5) Jul 31 Probably because it would require implementing and isn't as
- IchorDev (2/6) Jul 31 Could it re-use the logic of a static if-else chain?
- Nick Treleaven (9/16) Aug 01 It could, but then you couldn't do what monkyyy wants, and it
The equivalent of `static if`, but for `switch` statements. Is there any reason why we don't already have this?
Jul 27
On Monday, 28 July 2025 at 06:49:17 UTC, IchorDev wrote:The equivalent of `static if`, but for `switch` statements. Is there any reason why we don't already have this?Id suggest reusing template specailization logic ```d template foo(T...){ static switch(T){ case(int I,string S){ foo()=>iota(0,I).each!(()=>s.writeln); } case(S:int){ foo(int i)=>i.writeln; } case(S) if(isNumberic!S){ foo(S i)=>i.writeln("not int"); }} unittest{ foo!(10,"foo"); foo!int(10); foo(13.37);//inferred somehow } ```
Jul 28
On Monday, 28 July 2025 at 06:49:17 UTC, IchorDev wrote:The equivalent of `static if`, but for `switch` statements. Is there any reason why we don't already have this?Probably because it would require implementing and isn't as useful as static if and static foreach.
Jul 31
On Thursday, 31 July 2025 at 08:36:56 UTC, Atila Neves wrote:On Monday, 28 July 2025 at 06:49:17 UTC, IchorDev wrote:Could it re-use the logic of a static if-else chain?Is there any reason why we don't already have this?Probably because it would require implementing [...]
Jul 31
On Thursday, 31 July 2025 at 10:12:35 UTC, IchorDev wrote:On Thursday, 31 July 2025 at 08:36:56 UTC, Atila Neves wrote:It could, but then you couldn't do what monkyyy wants, and it wouldn't be particularly different from just using a static if-else chain except it would be less useful because it couldn't use `is` expressions. There's also the smaller issue of whether the syntax uses `break;` (which might look odd) or `static break`. If we get a `match` statement for pattern matching, then it would be more useful to have `static match` than `static switch`. But also probably harder to implement ;-)On Monday, 28 July 2025 at 06:49:17 UTC, IchorDev wrote:Could it re-use the logic of a static if-else chain?Is there any reason why we don't already have this?Probably because it would require implementing [...]
Aug 01
On Friday, 1 August 2025 at 10:32:19 UTC, Nick Treleaven wrote:you couldn't do what monkyyy wantsId rather have bool pattern matching runtime and ct; I suggest it because I expect to will lower well ```d static switch(T){ case(int I:1){...} case(int I:2){...} case(int I:3){...} } ``` which is verbose, repeating the int I over and over again. but I expect it will lower to: ```d mixin template __anonymous1(int I:1){...} mixin template __anonymous2(int I:2){...} mixin template __anonymous3(int I:3){...} static if(__traits(compiles,__anonymous1!(T))){ mixin __anonymous1!T;} else { static if(__traits(compiles,__anonymous2!(T))){ mixin __anonymous2!T;} else { static if(__traits(compiles,__anonymous3!(T))){ mixin __anonymous3!T;} else { static assert(false, T.stringof~"failed to match"); }}} ```
Aug 01
On Friday, 1 August 2025 at 10:32:19 UTC, Nick Treleaven wrote:There's also the smaller issue of whether the syntax uses `break;` (which might look odd) or `static break`.Yeah, plus `static goto case` and the problem of `final`, as D has a runtime error related to switch cases.
Aug 02
On Sunday, 3 August 2025 at 01:18:58 UTC, user1234 wrote:On Friday, 1 August 2025 at 10:32:19 UTC, Nick Treleaven wrote:I'd prefer a `static` prefix, rather than have a similar issue to `break`s always needing a label inside a `static foreach`.There's also the smaller issue of whether the syntax uses `break;` (which might look odd) or `static break`.Yeah, plus `static goto case`and the problem of `final``final` could always work the same way as at runtime. `default: static assert(0);`D has a runtime error related to switch cases.Yep, just make that a compile error.
Aug 19