Why we need compile time reflection
Reiner Pope
reiner.pope at REMOVE.THIS.gmail.com
Sun Oct 1 01:29:20 PDT 2006
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
More information about the Digitalmars-d
mailing list