Chances of D getting proper runtime reflection?

Jacob Carlborg doob at me.com
Sun Aug 21 23:33:56 PDT 2011


On 2011-08-21 21:07, Mehrdad wrote:
> On 8/21/2011 3:55 AM, 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.
>>
>
> It's not /quite/ the same thing as you're asking, but I'm facing a
> problem that seems to fundamentally prevent dynamic typing to work
> correctly (which is related, but of course not the same thing as,
> reflection):
> http://stackoverflow.com/questions/7102648/using-dynamic-typing-in-d-a-statically-typed-language
>
>
> As much as I'd love to see it happen, I can't see how templates and
> reflection would actually work at run-time.

Why don't you just use a variant ?

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list