static if enhancement

Wyatt via Digitalmars-d digitalmars-d at puremagic.com
Wed Jun 29 08:40:32 PDT 2016


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

I might be stepping on a land mine by bringing it up, but isn't 
this sort of thing what contracts are for?

-Wyatt


More information about the Digitalmars-d mailing list