On 21/08/2011 11:55, Jacob Carlborg 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.


You can actually do all of this now (albeit using a mixin), there's just no standard way of doing it. All you need to do is use compile time reflection to pull out all the fields/methods then store them for use at runtime. It would be good to get this functionality into phobos so there's a standard way of doing it though. The plus side to having to use a mixin is you don't get loads of bloat from classes you never want to reflect at runtime.

--
Robert
http://octarineparrot.com/

Reply via email to