Fun with allMembers
Georg Wrede
georg.wrede at iki.fi
Thu May 14 18:04:21 PDT 2009
Andrei Alexandrescu wrote:
> Shin Fujishiro wrote:
>> Hi,
>>
>> I've had fun with the allMembers traits over the past few days and found
>> it more powerful than I thought.
>>
>> __traits(allMembers, T) returns the member names of T. As some might
>> already know, T is not restricted to a class or struct; it can also be
>> an enum, template, or even module. Try this:
>> --------------------
>> enum E { a, b, c }
>> template T() { int x, y, z; }
>> import std.stdio;
>> pragma(msg, __traits(allMembers, E).stringof);
>> pragma(msg, __traits(allMembers, T).stringof);
>> pragma(msg, __traits(allMembers, std.stdio).stringof);
>> --------------------
>> You'll like the result :). It must be usable!
>>
>> For example, using allMembers with enums, I could implement
>> enumToString and enumFromString without defineEnum.
>> Code: http://codepad.org/HVvPjoI7
>>
>> So, what other uses could there be?
>
> Wow, I didn't know about this! It might as well be everything needed for
> a full-blown compile-time reflection package!
>
> To answer your question: for starters, try to implement BlackHole and
> WhiteHole as explained here:
>
> http://erdani.dreamhosters.com/cranking-policies-up.pdf
First of all, congratulations for conjuring up a cool (and
marketing-savvy name) for a concept!!!
Suppose I decide to use BlackHole as a base class. Would that mean that
I create the base class like
class RichardNixon createBlackHoleClass!()
class DerivedRN : RichardNixon {
/* something declared here, makes no difference in this example */
int foo(int ip) {/* whatever */}
}
and then, I'd expect it to work like
void main() {
DerivedRN drn = new DerivedRN;
float ivar, jvar;
jvar = drn.bar(ivar); // note: bar, not foo
}
and it'd work like RichardNixon were actually defined as
class RichardNixon {
float bar(float jv) { return float 0; } // or whatever
}
More information about the Digitalmars-d
mailing list