Mixin Aspect-Orientation: Comments please!

Tony tq at tq.tq
Sun Jan 16 15:34:01 PST 2011


Here is a quick outline of an idea of how the aspect-oriented programming paradigm could be incorporated into D. What I’d like to propose is picking only the most useful features of AOP while keeping D as lean as it is.
The big promise of AOP is the potential of keeping your code very clean even as it grows by having your orthogonal concerns kept apart. The extensions to the current D syntax that would be needed may not be that many:



Suppose you have this class:

class Foo
{
      int fun( int x )
      {
      }
}



Now let’s create some advice. Advice is code that is injected to handle some specific concern. One reason complex programs easily become cluttered is that they usually have many different concerns with little bits and pieces of them everywhere. AOP solves this by abstracting concerns into separate units instead of having each concern scattered all over the code. It’s a kind of modularity.


mixin template AdviceX()
{
      int fun( int x )
      scope( entry )
      {
      }
      scope( exit )
      {
      }
      scope( failure )
      {
            // undo changes here.
      }
      scope( success )( returnValue )
      {
            //  const access to return value.
      }
}


The new thing here would be the ability to append statements to the beginning and end of a scope. By using the excellent scope construct different advice are kept separate and yet well integrated with the rest of the code. Any exception will be dealt with properly.


Here is how you would include the mixin advice:

class Foo
{
      mixin AdviceX;
      mixin AdviceY;

      int fun( int x )
      {
            // not replaced.
      }
}


The order in which the mixin advice are specified determines their nesting order.


Please comment!


More information about the Digitalmars-d mailing list