Chances of D getting proper runtime reflection?

Jacob Carlborg doob at me.com
Sun Aug 21 03:55:52 PDT 2011


What are the chances of D getting proper runtime reflection? Something 
like this:

class Foo
{
     private int a;

     private void bar (int i)
     {
         a = i;
     }
}

auto foo = new Foo;
Object o = foo;
o.setInstanceVariable("a", 3);
assert(foo.a == 3);

This would be useful as well:

o.send("bar", 5);
assert(foo.a == 5);
assert(foo.a == o.getInstanceVariable("a"));

Currently I have most use of the first example. These example should 
work regardless if the fields/methods are private, protected or public.

As far as I know this wouldn't require any changes to the language, only 
the compiler and runtime would need to be modified. To me, it doesn't 
look that complicated to implement, the runtime just needs to store an 
associative arrays that maps names of instance variables and methods to 
their addresses. This could probably be stored in the ClassInfo. 
Something like this:

Foo.classinfo.setInstanceVariable(foo, "a", 3);

And this could be shorted to:

o.setInstanceVariable("a", 3);

"setInstanceVariable" could just forward the call to the class info.

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list