Traits

Artur Skawina art.08.09 at gmail.com
Sat Oct 12 16:48:46 PDT 2013


On 10/12/13 21:42, luminousone wrote:
> On Saturday, 12 October 2013 at 11:50:05 UTC, Artur Skawina wrote:
>>    template isBaseOf(BASE, C) {
>>       enum isBaseOf = {
>>          static if (is(C S == super))
>>             foreach (A; S)
>>                static if (is(A==BASE))
>>                   return true;
>>          return is(C==BASE);
>>       }();
>>    }
> 
> I like that! Avoids importing std.traits, And will correctly handle interfaces as well.

It's also buggy. A more useful version would be:

   template isBaseOf(BASE, C) {
      enum isBaseOf = {
         static if (is(C S == super))
            foreach (A; S)
               static if (isBaseOf!(BASE, A))
                  return true;
         return is(C==BASE);
      }();
   }

Sorry, didn't test this properly; going to blame the dlang is-expression docs.
It's not like D has multiple inheritance, so using "base classes" (plural)
is very misleading...

artur


More information about the Digitalmars-d-learn mailing list