www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Why we need compile time reflection

Reflection is obviously useful in some situations, but it bloats the 
binary output files with the required metadata. A possible solution to 
this which I think is very promising is compile time reflection (CTR), 
which makes this information available only at compile time, accessible 
through compile-time metaprogramming (templates at the moment, but I 
have some ideas about making the system slightly easier). Since D has 
strong metaprogramming capabilities because of the template system, it 
is well suited for compile time reflection. There are a few reasons 
which make compile-time reflection compelling:
   - a lot (even most?) of reflection is used for metaprogramming (eg 
serialization). This can (and for efficiency purposes, should) be done 
at compile time
   - compile time reflection adds no bloat to the compiled output
   - clever metaprogramming can in fact harness CTR to selectively 
export information about classes, thus facilitating runtime reflection, 
but only where explicitly required. This could allow things like this:
   class Foo { mixin SymbolExporter; /* This adds a few functions to the 
class, such as ListMethods(), ListFields(), etc. */ }
   or
   auto methodInfo = ListMethodsOf!(SomeExternalNonExportingClass);
   - CTR more expressive than runtime expression, and it is still 
statically type-checked; type errors are caught at compile-time 
(excluding cast(Foo), of course)

Just food for thought,

Reiner
Oct 01 2006