D Template based paser generator in 137 loc

BCS nothing at pathlink.com
Fri Jan 5 11:08:04 PST 2007


Don Clugston wrote:
> 
> A beautiful piece of work.
> The use of default template value arguments to distinguish actions is an 
> act of genius.
> 
> Incidentally, the technique can be used for classes as well, allowing 
> you to instantiate a class from a string literal.
> There's lots of potential here.

SWEET!!!

> ---
> class ReflectableObject(char [] reflectionName) {
>     static assert(0, "Undeclared class");
> }
> 
> class ReflectableObject(char [] reflectionName : "Animal") {
>   int numberOfLegs;
> }
> 
> class ReflectableObject(char [] reflectionName : "Dog") : 
> ReflectableObject!("Animal") {
>  int dog;
> }
> 
> class ReflectableObject(char [] reflectionName : "Cat") : 
> ReflectableObject!("Animal")  {
>  int cat;
> }
> 
> 
> void main()
> {
>    auto x = new ReflectableObject!("Dog");
>    x.numberOfLegs=7;
>    x.dog = 53;
> 
> // OK: animal is a base class of Dog.
>   ReflectableObject!("Animal") y = x;
> 
>   // Won't compile, because a Dog isn't a Cat.
>   ReflectableObject!("Cat") z = x;
> }



More information about the Digitalmars-d-announce mailing list