Aspect oriented programming: Difference between revisions

→‎Tcl: Added discussion/implementation
(mark for attention)
(→‎Tcl: Added discussion/implementation)
Line 113:
=={{header|Python}}==
Python has special syntax for [http://legacy.python.org/dev/peps/pep-0318/ decorators] acting on functions and methods, as well as [http://stackoverflow.com/questions/100003/what-is-a-metaclass-in-python metaclasses].
 
=={{header|Tcl}}==
Tcl's <code>trace</code> command enables much AOP-like functionality, with variable traces allowing interception of accesses to variables, and command traces allowing interception of executions of procedures, etc. In addition, TclOO (Tcl's <!--main--> object system) also supports filter methods, which wrap around invocations of methods of an object (or all methods of a class) as a generalisation of before, after and around advice. This works well when combined with a mixin, allowing interception to be applied on a per-instance basis if desired.
 
For example:
<lang tcl>oo::class create InterceptAspect {
filter <methodCalled>
method <methodCalled> args {
puts "[self] - called '[self target]' with '$args'"
set result [next {*}$args]
puts "[self] - result was '$result'"
return $result
}
}
 
oo::class create Example {
method calculate {a b c} {
return [expr {$a**3 + $b**2 + $c}]
}
}
 
Example create xmpl
puts ">>[xmpl calculate 2 3 5]<<"
oo::objdefine xmpl mixin InterceptAspect
puts ">>[xmpl calculate 2 3 5]<<"</lang>
{{out}}
<pre>
>>22<<
::xmpl - called '::Example calculate' with '2 3 5'
::xmpl - result was '22'
>>22<<
</pre>
Anonymous user