xxxxxxxxxx
import std.stdio;
class A {}
class B { A a; }
class C { B b; }
class D { C c; }
class E { D d; }
string createCondition(string[] entries)
{
string result = "";
string lastEntry = "";
foreach (entry; entries)
{
if (lastEntry && lastEntry.length) lastEntry ~= "." ~ entry;
else lastEntry = entry;
result ~= lastEntry ~ " !is null &&";
}
result.length -= 2;
return result;
}
bool isDefined(alias symbol,T)(T arg)
{
import std.array : split;
enum symbolEntries = symbol.split(".");
enum entriesCondition = createCondition(["arg"] ~ symbolEntries[1..$]);
mixin("return " ~ entriesCondition ~ ";");
}
void main()
{
auto e = new E;
e.d = new D;
e.d.c = new C;
e.d.c.b = new B;
e.d.c.b.a = new A;
if (isDefined!((e.d.c.b.a).stringof)(e))
{
writeln("e.d.c.b.a is defined.");
}
else
{
writeln("e.d.c.b.a is not defined.");
}
e = new E;
e.d = new D;
e.d.c = new C;
if (isDefined!((e.d.c.b.a).stringof)(e))
{
writeln("e.d.c.b.a is defined.");
}
else
{
writeln("e.d.c.b.a is not defined.");
}
}