Reflection: is type an inner class

Tyler Jameson Little beatgammit at gmail.com
Sat Oct 20 20:44:48 PDT 2012


I hope this isn't a double post, I'm posting from the web ui. I 
got this working using __traits(compiles):

     A a = new A;
     static if (is(A == class)) {
         alias TypeTuple!(A, BaseClassesTuple!A) Types;
     } else {
         alias TypeTuple!A Types;
     }

     foreach (BT; Types) {
         foreach (i, type; typeof(BT.tupleof)) {
             enum name = BT.tupleof[i].stringof[1 + 
BT.stringof.length + 2 .. $];

             if (!mixin("ret." ~ name)) {
                 static if (__traits(compiles, mixin("BT." ~ 
type.stringof))) {
                     mixin("a." ~ name) = ret.new type;
                 } else {
                     mixin("a." ~ name) = new type;
                 }
             }
         }
     }

This is the basic idea. I've omitted a bunch of the irrelevant 
code for brevity.  The following structure gets initialized 
correctly:

     class A {
         class B {
             class C {
                 int d;
             }
             C c;
         }
         B b;
     }

Is this the best way to do this, or is there a cleaner, type 
independent way?  I'm doing this for a JSON marshaller I'm 
working on.


More information about the Digitalmars-d-learn mailing list