Composing features at compile time

Dejan Lekic dejan.lekic at gmail.com
Sun Nov 24 10:28:59 PST 2013


Jacob Carlborg wrote:

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

Jakob, whenever I need something like you describe, I do more/less the same what 
is described on this Wikipedia page: 
http://en.wikipedia.org/wiki/Composition_over_inheritance . C# example is 
exactly how I do this (in Java and D).

-- 
Dejan Lekic
dejan.lekic (a) gmail.com
http://dejan.lekic.org


More information about the Digitalmars-d-learn mailing list