On Wednesday, 7 May 2014 at 16:51:10 UTC, amehat wrote:
D  an not do reflection

It can, but it is different than Java. D can look at class members at compile time, and can build up runtime tables, but it takes a few tricks to do things like get methods of a derived class.

it is limited to the Run-Time Type Information (RTTI ) (http://dlang.org/traits.html).

That's actually compile time introspection docs, the run time stuff is found in http://dlang.org/phobos/object.html#TypeInfo (and is useless for looking at methods btw)


To find methods that take two arguments, you'd do something like this:

alias helper(alias A) = A; // just to shorten things later

foreach(memberName; __traits(allMembers, YourClass)) {
alias member = helper!(__traits(getMember, YourClass, memberName));
    static if(is(member == function)) {
         import std.traits;
         ParameterTypeTuple!member args;
         static if(args.length == 2) {
                // found one
               if(memberName == "accessibleProc") {
                   // you can check the name too
               }
         }
    }
}


tbh, I don't know exactly what the Java does but the D things I used are documented here: http://dlang.org/phobos/std_traits.html#ParameterTypeTuple and http://dlang.org/traits.html#allMembers

the sample chapter for my D book isn't available yet :( I covered this stuff in more depth there.

But actually calling the member gets a bit tricky since you need to attach this to it. You'd do __traits(getMember, some_instance_object, memberName)(args...).

That can also be wrapped in a delegate to transform arguments, box/unbox, and such generically too, which is probably closer to what Java does. I don't think this is in the standard library, you'd have to DIY though.


Again, this won't just work with child classes, so it might not even be what you need. To work with child classes, you'd want to build an array of the delegates for runtime use or something, and also generate them for each derived instance in the module that defines them.

If this is looking like it might be what you need, I can help with that too.

Reply via email to