On Sun, 21 Aug 2011 06:55:52 -0400, Jacob Carlborg <d...@me.com> wrote:

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.


I've basically done this entirely in library code: see 
https://jshare.johnshopkins.edu/rjacque2/public_html/variant.mht

Reply via email to