__traits and string mixins

Christian Köstlin via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Dec 3 05:36:16 PST 2015


Hi,

I started an experiment with the informations that are available for 
compile time reflection.

What I wanted to create is a thor like cli parser library, that forces 
you to encapsulate your programs into subclasses of Dli. The commands
and options, that are understood by the generated cli are taken from the 
members, that have to be enhanced with UCA's. The __traits facility is 
used to generate from those a string mixin, that also has to be added to 
your class. A sample usage would look like this:

class Git : Dli {
   @Option string verbose;

   @Task void helloWorld(string option1, string option2) {
     ...
     your code here
     ...
   }
   mixin(createDli!(Git));
}

this program should be able to understand:

./git --verbose helloWorld --option1=pretty --option2=whatever

When I created this stuff the __traits stuff got me thinking, because I 
iterate e.g. allMembers of a class, but the result of this code is mixed 
in again into the class (and creates new members on the way).

So how exactly is this done compiler wise?

Another question that came up for me is: would it not be awesome, if the 
whole AST would be available in traits? The facilities as they are, are 
already a little bit more powerful than e.g. java reflection, because 
you can e.g. get to the name of parameters of method.

thanks for your input,

christian

p.s.: if you are interested in a very rough first version of the code or 
if you have enhancements for it, please have a look at: 
https://github.com/gizmomogwai/dli



More information about the Digitalmars-d-learn mailing list