Composing features at compile time

Dicebot public at dicebot.lv
Sun Nov 24 09:31:24 PST 2013


On Sunday, 24 November 2013 at 17:25:49 UTC, Dicebot wrote:
> Don't really get the question. Isn't it what policies do pretty 
> much by definition? You decompose part of target functionality 
> into smaller blocks with optional behavior (including no-op 
> behavior) and provide specific ones as alias parameters. D has 
> additional benefit of being able to use one as template mixin 
> for cleaner syntax. But I am pretty sure you know this, so what 
> is the question again? :)

To clarify, OP example will look like this:

class Foo (alias Logger)
     if (isLoggger!Logger)
{
      private Logger logger;

      this ()
      {
          logger = new Logger;
      }

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

// ...

auto foo = new Foo!StubLogger(); // "false" case

It works better with compilers like LDC of course, because of 
good inlining and AST restructuring during optimization pass.


More information about the Digitalmars-d-learn mailing list