www.digitalmars.com         C & C++   DMDScript  

digitalmars.dip.ideas - static switch statement

reply IchorDev <zxinsworld gmail.com> writes:
The equivalent of `static if`, but for `switch` statements.

Is there any reason why we don't already have this?
Jul 27 2025
next sibling parent monkyyy <crazymonkyyy gmail.com> writes:
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 2025
prev sibling parent reply Atila Neves <atila.neves gmail.com> writes:
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 2025
parent reply IchorDev <zxinsworld gmail.com> writes:
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:
 Is there any reason why we don't already have this?
Probably because it would require implementing [...]
Could it re-use the logic of a static if-else chain?
Jul 31 2025
parent reply Nick Treleaven <nick geany.org> writes:
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:
 On Monday, 28 July 2025 at 06:49:17 UTC, IchorDev wrote:
 Is there any reason why we don't already have this?
Probably because it would require implementing [...]
Could it re-use the logic of a static if-else chain?
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 ;-)
Aug 01 2025
next sibling parent monkyyy <crazymonkyyy gmail.com> writes:
On Friday, 1 August 2025 at 10:32:19 UTC, Nick Treleaven wrote:
 you couldn't do what monkyyy wants
Id 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 2025
prev sibling parent reply user1234 <user1234 12.de> writes:
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 2025
parent IchorDev <zxinsworld gmail.com> writes:
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:
 There's also the smaller issue of whether the syntax uses 
 `break;` (which might look odd) or `static break`.
Yeah, plus `static goto case`
I'd prefer a `static` prefix, rather than have a similar issue to `break`s always needing a label inside a `static foreach`.
 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 2025