Thread overview
`static switch` statement?
Sep 17, 2019
octo
Sep 17, 2019
Max Haughton
Sep 17, 2019
jmh530
September 17, 2019
`static switch` would make a nice addition to `static if`, `static foreach`, etc.

Any thoughts?
September 17, 2019
On Tuesday, 17 September 2019 at 00:41:03 UTC, octo wrote:
> `static switch` would make a nice addition to `static if`, `static foreach`, etc.
>
> Any thoughts?

Could it be done as static foreach (blah; blah) {static if (...) {} }? I don't know whether the amount of use it would get would be useful relative to the cost of implementing it
September 16, 2019
On 9/16/19 6:41 PM, octo wrote:
> `static switch` would make a nice addition to `static if`, `static foreach`, etc.
> 
> Any thoughts?

It doesn't seem to add power to the language. "static try" would be way better because a typical idiom is:

static if (__traits(compiles, { codecodecode }))
{
    codecodecode
}

It would be way nicer if we had something like:

static try
{
    codecodecode
}
catch (Exception e)
{
    // e.msg contains the compile-time error
}

September 17, 2019
On Tuesday, 17 September 2019 at 01:05:28 UTC, Andrei Alexandrescu wrote:
> On 9/16/19 6:41 PM, octo wrote:
>> `static switch` would make a nice addition to `static if`, `static foreach`, etc.
>> 
>> Any thoughts?
>
> It doesn't seem to add power to the language. [snip]

Wouldn't it add the comparable amount of power on the compile-side that the run-time switch statement adds? Run-time switch can be reconstructed as a series of if statements, same thing as compile-time switch.

I am not sure I would use the feature, but it reminds me of some attributes being preprended with @ and others not. It's just one of those quirks of the language.