Thread overview
Runtime reflection
Sep 18, 2007
Jascha Wetzel
Sep 18, 2007
Ingo Oeser
Sep 19, 2007
Jascha Wetzel
Sep 20, 2007
Craig Black
Sep 20, 2007
Jascha Wetzel
Sep 21, 2007
dennis luehring
Sep 21, 2007
Jascha Wetzel
September 18, 2007
Here is a simple way of adding __traits based runtime reflection to D classes:

http://mainia.de/classinfoex.d
(requires DMD 2.004)

It does not increase the class instance size.
All extra info is stored statically.

See the doc-comment for details.
September 18, 2007
Jascha Wetzel wrote:

> Here is a simple way of adding __traits based runtime reflection to D classes:

Wow! If that works, it is really a piece of art!

- small, efficient
- has constant runtime cost (speed AND size)
- classes which don't want to be reflectable
  don't pay the cost
- implementation is simple and obvious


Best Regards

Ingo Oeser
September 19, 2007
Jascha Wetzel wrote:
> Here is a simple way of adding __traits based runtime reflection to D classes:
> 
> http://mainia.de/classinfoex.d
> (requires DMD 2.004)
> 
> It does not increase the class instance size.
> All extra info is stored statically.
> 
> See the doc-comment for details.

i have updated the file.
dynamic calls are now supported:

class A
{
    mixin Reflect;
    int foo() { return 1234; }
    void bar(string s) { writefln("%s", s); }
}

A o = new A;
call(o, "bar", null, "argument string");
Box retval;
call(o, "foo", &retval);
writefln("foo returned %s", retval);
September 20, 2007
"Jascha Wetzel" <firstname@mainia.de> wrote in message news:fcrrk4$pb7$1@digitalmars.com...
> Jascha Wetzel wrote:
>> Here is a simple way of adding __traits based runtime reflection to D classes:
>>
>> http://mainia.de/classinfoex.d
>> (requires DMD 2.004)
>>
>> It does not increase the class instance size.
>> All extra info is stored statically.
>>
>> See the doc-comment for details.
>
> i have updated the file.
> dynamic calls are now supported:
>
> class A
> {
>     mixin Reflect;
>     int foo() { return 1234; }
>     void bar(string s) { writefln("%s", s); }
> }
>
> A o = new A;
> call(o, "bar", null, "argument string");
> Box retval;
> call(o, "foo", &retval);
> writefln("foo returned %s", retval);

Impressive!  I am surprised at how little code is required.

I remember something about traits not distinguishing between overloaded functions.  Is this still the case or was this fixed in 2.004?  Are there any other problems with traits that you have encountered? 

September 20, 2007
Craig Black wrote:
> Impressive!  I am surprised at how little code is required.
> 
> I remember something about traits not distinguishing between overloaded functions.  Is this still the case or was this fixed in 2.004?  Are there any other problems with traits that you have encountered?

__traits does distinguish overloaded functions. But it does not associate functions that override an inherited function with the class they're implemented in. That is, there appears to be no way to list all functions that are implemented by the given class (overridden inherited or not) without also listing all inherited ones. derivedMembers only lists non-overridden functions.

Another, maybe related thing is, that there appears to be no way to tell whether a function has an implementation or not. classinfoex tries to get the address of any function it finds in the static constructor, which will lead to linker errors (no body => no address).

Otherwise, __traits is fine. classinfoex isn't using __traits very exhaustively, though. All it does is saving a TypeInfo with the name and address/offset of the symbol. The rest is drawn from TypeInfos.
TypeInfo is more trouble. working around the missing parameter TIs in TypeInfo_Function is pretty extensive, and classinfoex is buggy there.
September 21, 2007
Jascha Wetzel schrieb:
> Here is a simple way of adding __traits based runtime reflection to D classes:
> 
> http://mainia.de/classinfoex.d
> (requires DMD 2.004)
> 
> It does not increase the class instance size.
> All extra info is stored statically.
> 
> See the doc-comment for details.

do you see way to extend your lib with the ability
to "see" the derivation-hierachy?

ciao dennis
September 21, 2007
dennis luehring wrote:
> Jascha Wetzel schrieb:
>> Here is a simple way of adding __traits based runtime reflection to D classes:
>>
>> http://mainia.de/classinfoex.d
>> (requires DMD 2.004)
>>
>> It does not increase the class instance size.
>> All extra info is stored statically.
>>
>> See the doc-comment for details.
> 
> do you see way to extend your lib with the ability
> to "see" the derivation-hierachy?
> 
> ciao dennis

atm, i don't see a way. the current classinfoex version adds all members to a class' reflection data. that way it's enough to add "mixin Reflect;" to a derived class.