www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - code generation for AOP-style code

With code generation it becomes easy to write code reminiscing of AOP 
style. For example, given a class Widget, it's easy to generate a class 
that traces absolutely all uses of Widget:

alias Trace!(Widget) MyWidget;

But the goodies don't stop there. AOP allows intercepting function calls 
by using (surprise!) regexp matching (see e.g. 
http://www.java2s.com/Code/Java/Spring/RegexpPointcutExample.htm). D can 
do that by using exactly the compile-time regex that is being discussed 
around here:

// Trace only methods starting with Set
alias Trace!(Widget, regex!("Set.*")) MyWidget;

More sophisticated libraries can define an entire annotation language 
for defining AOP facilities, the simplest being simple implementations 
of the NVI idiom (http://www.gotw.ca/publications/mill18.htm).


Andrei
Feb 10 2007