Need reflection facilities

Kirk McDonald kirklin.mcdonald at gmail.com
Wed Jan 3 13:25:49 PST 2007


Dave Woldrich wrote:
> Hello all, I read on the realtime type information how-to page that there are
> some runtime type information facilities in D.
> (http://www.prowiki.org/wiki4d/wiki.cgi?action=browse&id=HowTo/RealtimeTypeInformation)
> 
> I am the author of CeeFIT (http://ceefit.woldrich.com), the C++ implementation
> of the Framework for Integrated Test (http://fit.c2.com).  It was extremely
> painful to implement portable reflection capabilities into C++ to mimic what
> goes on in Java.  Basically I used every function pointer, template, macro,
> and static linker trick I knew to pull it off (and it's still fugly.)
> 

D's reflection support is certainly better than C++'s. However, it is 
not quite as powerful as you are wishing it is. :-)

> If I were to implement fit for D (DeeFIT?) it would be extremely helpful to
> have Class.forName() functionality out of the box as well as the ability to
> get pointers-to-function and references to fields by name as well for a given
> object.
> 

These are not possible directly. Some other things are:

* The reverse operation, getting the name of a class at runtime, is 
possible. Every Object instance has a "classinfo" property, which in 
turn has a "name" property. (It also have a "vtbl" property, of type 
void*[], which might prove useful for this kind of trickery.) Or stop 
listening to me, and read the docs: :-)
http://digitalmars.com/d/phobos/object.html

* D is remarkably easy to parse, especially compared to C++. If you have 
to, parsing user code to generate specialized code can be done in a much 
more robust way than C++ can dream of.

* D's /compile-time/ reflection abilities kick the pants off C++. Search 
around for Don Clugston's "Nameof" module. It allows you to get the name 
of just about any symbol at compile time. (The reverse operation of 
getting a symbol from its name is still not possible, however.)

> Would it be possible for the D compiler to generate some table of symbols or
> synthetically generated reflection module and provide that for linkage into
> the final binary?  Maybe the compiler could employ the same sorts template
> specialization and static initialization techniques I used in CeeFIT to
> generate such artifacts.  Clearly such a table could double the size of the
> resulting .exe, but not like I care.
> 

As I said, parsing this information out yourself is almost /easy/ 
compared to C++. It might also be worth pointing out that strings are 
allowed as template parameters in D. Also, I once again suggest you look 
at Don Clugston's Nameof (and Demangle). It's a fine bit of work, which 
appears to do half of this problem already.

-- 
Kirk McDonald
Pyd: Wrapping Python with D
http://pyd.dsource.org



More information about the Digitalmars-d mailing list