Composing features at compile time

Jacob Carlborg doob at me.com
Sun Nov 24 05:18:14 PST 2013


Does anyone know a good way of composing features at compile time? Say I 
have a struct or class that I want to support different features that 
are configurable at compile time. One way would be to just pass in a 
couple of boolean flags and use static-if, like this:

class Foo (bool logging, bool events)
{
     static if (logging)
         Logger logger;

     this ()
     {
         static if (logging)
             logger = new Logger;
     }

     void action ()
     {
         static if (logging)
             logger.info("performing action");
         // perform action
     }
}

Using this approach I get the feeling that there will quickly become a 
big nest of static-ifs. Does anyone have any better ideas?

-- 
/Jacob Carlborg


More information about the Digitalmars-d-learn mailing list