Introduction to traits (and __traits)
Ali Çehreli
acehreli at yahoo.com
Fri Aug 30 14:06:41 PDT 2013
On 08/30/2013 01:57 PM, Joseph Rushton Wakeling wrote:
> On 30/08/13 21:40, Benjamin Thaut wrote:
>> You need to put it into a static if, otherwise the compiler will continue
>> semantic checks on the second part of the expression. E.g.
>>
>> template isGraph(G)
>> {
>> static if(__traits(hasMember, G, "directed"))
>> enum bool isGraph = isBoolean!(typeof(G.directed));
>> else
>> enum bool isGraph = false;
>> }
>
> Ahh, right, thanks. :-)
>
> Is there a recommended way for handling the case where there are many
> such members -- say about 10 or more? The static if's could become very
> highly nested with this approach. I suppose I could go through like this:
>
> static if(!__traits(hasMember, G, "one"))
> enum bool isGraph = false;
> else static if(!__traits(hasMember, G, "two"))
> enum bool isGraph = false;
> else static if ...
> ...
> else
> {
> // Now I know all the members exist and I can
> // start checking out their properties ...
> }
>
How about allSatisfy:
http://dlang.org/phobos/std_typetuple.html#.allSatisfy
Ali
More information about the Digitalmars-d-learn
mailing list