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.