static if enhancement

Steven Schveighoffer via Digitalmars-d digitalmars-d at puremagic.com
Fri Jun 24 11:27:07 PDT 2016


On 6/24/16 1:54 PM, Andrei Alexandrescu wrote:
> On 6/24/16 1:15 PM, Steven Schveighoffer wrote:
>> The problem that hasn't been made clear is, why can't you just write:
>>
>> static if(condition)
>> {
>>     ... // some code
>>     return;
>> }
>>
>> // some more code
>>
>> And the answer is, I'm guessing, bug 14835 -- misguided "unreachable
>> statement" warnings.
>>
>> Should we mark your bug as a duplicate?
>
> Sorry, the problem is that the code doesn't compile at all if the static
> if is false. So I need to insert the "else".

Even with this, I still didn't understand. Now with your example in the 
bug report, it's clear. Reproducing here:

void fun(T)(T obj)
{
     static if (!hasMember(T, "gun")) throw new Exception("No gun");
     obj.gun;
}

Call with something that doesn't have a gun member, and even without the 
reachability warnings (no -w switch), it doesn't compile. However, with 
an else clause, it would compile.

> The compiler should not attempt at all to compile the code after the
> static if.

Right.

-Steve


More information about the Digitalmars-d mailing list