code generation for AOP-style code
    Andrei Alexandrescu (See Website For Email) 
    SeeWebsiteForEmail at erdani.org
       
    Sat Feb 10 23:20:32 PST 2007
    
    
  
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
    
    
More information about the Digitalmars-d
mailing list