static if enhancement

jmh530 via Digitalmars-d digitalmars-d at puremagic.com
Fri Jun 24 13:00:43 PDT 2016


On Friday, 24 June 2016 at 18:27:07 UTC, Steven Schveighoffer 
wrote:
>
> 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.
>

It should be (!hasMember!(T, "gun"))

But otherwise, yes, it seems unintuitive to require the else 
statement. Your point about types not having the gun member 
failing to compile is what I found confusing. By contrast, when 
the type has a gun member, it compiles fine. I changed it to 
return obj.gun and it was fine. So I wouldn't characterize this 
as a situation where the code below is ignored. I would 
characterize it as code that should be ignored is otherwise 
compiled.


More information about the Digitalmars-d mailing list