Runtime reflection costs

Sergey Gromov snake.scaly at gmail.com
Sun Sep 28 07:10:18 PDT 2008


Sun, 28 Sep 2008 09:25:38 -0400,
Christopher Wright wrote:
> Now I'm thinking about runtime reflection. D doesn't support significant 
> runtime reflection -- you can invoke a default constructor, and that's it.

D2's ClassInfo (2.019) has getMembers() which returns an array of 
MemberInfo, each is an instance of either MemberInfo_field or 
MemberInfo_function, containing all the info you need.  Although it 
doesn't work:

module test;
import std.stdio: writefln;
class A
{
    int i;
    this() {i = 3;}
    this(int f) {i = f;}
    int foo() {return i;}
}
void main() {
    auto ci = ClassInfo.find("test.A");
    writefln("found ", ci.name);            // prints "found test.A"
    auto m = ci.getMembers(null);
    writefln("has ", m.length, " members"); // prints "has 0 members"
}

But the principal mechanism is in place, it just needs to be 
fixed/implemented.



More information about the Digitalmars-d mailing list