www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.announce - Runtime reflection

reply Jascha Wetzel <firstname mainia.de> writes:
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.
Sep 18 2007
next sibling parent Ingo Oeser <ioe-news rameria.de> writes:
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
Sep 18 2007
prev sibling next sibling parent reply Jascha Wetzel <firstname mainia.de> writes:
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);
Sep 19 2007
parent reply "Craig Black" <craigblack2 cox.net> writes:
"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?
Sep 19 2007
parent Jascha Wetzel <firstname mainia.de> writes:
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.
Sep 20 2007
prev sibling parent reply dennis luehring <dl.soluz gmx.net> writes:
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
Sep 20 2007
parent Jascha Wetzel <firstname mainia.de> writes:
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.
Sep 21 2007