D Template based paser generator in 137 loc

Don Clugston dac at nospam.com.au
Fri Jan 5 02:43:10 PST 2007


BCS wrote:
> This is a totaly template based system. No external utilities are 
> needed. (* the 137 loc is after removal of all unneeded line)
[snip]

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.
---
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