Aspect oriented programming: Difference between revisions
Added Wren
Thundergnat (talk | contribs) m (Thundergnat moved page Aspect Oriented Programming to Aspect oriented programming: Follow normal task title capitalization policy) |
(Added Wren) |
||
Line 251:
::xmpl - result was '22'
>>22<<
=={{header|Wren}}==
Wren has no support for AOP as such, either built-in or (AFAIK) via third parties. Nor does it have mixins.
However, our simple but flexible module system - a module is just a Wren source code file - makes it easy to wrap one class inside another in a similar fashion to the Julia example.
This enables us to add functionality to a class without interfering with its source code.
Notice also that a client can still import the ''wrapped'' class via the ''wrapper'' module which enables it to call ''unwrapped'' methods without the need to import the ''wrapped'' module as well. To expand the Julia example a little:
<lang ecmascript>/* adder.wren */
class Adder {
static add2(x) { x + 2 }
static mul2(x) { x * 2 }
}</lang>
<lang ecmascript>/* logAspectHeader.wren */
import "/adder" for Adder
var Start = System.clock // initialize timer for logging
class LogAspectAdder {
static log(s) {
var elapsed = ((System.clock - Start) * 1e6).round
System.print("After %(elapsed) μs : %(s)")
}
static add2(x) {
log("added 2 to %(x)")
return Adder.add2(x)
}
}</lang>
<lang ecmascript>/* adderClient.wren */
import "/logAspectAdder" for LogAspectAdder, Adder
var a = LogAspectAdder.add2(3)
var m = Adder.mul2(4)
System.print("3 + 2 = %(a)") // logged
System.print("4 * 2 = %(m)") // not logged</lang>
{{out}}
Running the client, we get:
<pre>
After 44 μs : added 2 to 3
3 + 2 = 5
4 * 2 = 8
</pre>
|