Introspecting a Module with Traits, allMembers
Maxime Chevalier-Boisvert via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed Jul 9 13:52:28 PDT 2014
I got the following code to do what I want:
static this()
{
void addOp(ref Opcode op)
{
assert (
op.mnem !in iir,
"duplicate op name " ~ op.mnem
);
iir[op.mnem] = &op;
}
foreach (memberName; __traits(allMembers, ir.ops))
{
static if (__traits(compiles, addOp(__traits(getMember,
ir.ops, memberName))))
{
writeln(memberName);
addOp(__traits(getMember, ir.ops, memberName));
}
}
}
It's a bit of a hack, but it works. Is there any way to create
some sort of alias for __traits(getMember, ir.ops, memberName) so
that I don't have to write it out in full twice? Made some
attempts but only got the compiler to complain.
More information about the Digitalmars-d-learn
mailing list