This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
at:tutorial:reflection [2009/11/30 16:54] dharnie *prog->soft |
at:tutorial:reflection [2010/11/16 16:32] (current) tvcutsem |
||
---|---|---|---|
Line 4: | Line 4: | ||
The reflective model of AmbientTalk is based on [[http:// | The reflective model of AmbientTalk is based on [[http:// | ||
+ | |||
===== Mirrors ===== | ===== Mirrors ===== | ||
Line 45: | Line 46: | ||
> def x : | > def x : | ||
>> < | >> < | ||
- | > x() | ||
- | >> 2 | ||
// and we can also invoke methods reflectively: | // and we can also invoke methods reflectively: | ||
> mirrorOnP.invoke(p, | > mirrorOnP.invoke(p, | ||
Line 85: | Line 84: | ||
===== Mirages ===== | ===== Mirages ===== | ||
- | Extending the AmbientTalk core language | + | Extending the AmbientTalk core language |
As a simple example, we show how to trace all method calls made on an object. The first step is to define a //mirror// object that encapsulates this logging behaviour. A mirror object must implement the complete AmbientTalk MOP. To make it convenient to make small changes to the MOP, AmbientTalk provides the '' | As a simple example, we show how to trace all method calls made on an object. The first step is to define a //mirror// object that encapsulates this logging behaviour. A mirror object must implement the complete AmbientTalk MOP. To make it convenient to make small changes to the MOP, AmbientTalk provides the '' | ||
Line 91: | Line 90: | ||
< | < | ||
def createTracingMirror(baseObject) { | def createTracingMirror(baseObject) { | ||
- | extend: defaultMirror with: { | + | extend: defaultMirror.new(baseObject) |
def invoke(slf, invocation) { | def invoke(slf, invocation) { | ||
system.println(" | system.println(" | ||
Line 100: | Line 99: | ||
</ | </ | ||
- | The primitive '' | + | The primitive '' |
< | < | ||
- | def createTracingMirror(baseObject) { | + | def TracingMirror := mirror: { |
- | | + | def invoke(slf, invocation) { |
- | def invoke(slf, invocation) { | + | system.println(" |
- | system.println(" | + | super^invoke(slf, |
- | super^invoke(slf, | + | |
- | } | + | |
} | } | ||
} | } | ||
Line 121: | Line 118: | ||
</ | </ | ||
- | In the code above, the closure passed to '' | + | In the code above, the closure passed to '' |
- | {{:at:tutorial:meta-2.jpg|: | + | Another alternative is to just pass a mirror prototype to '' |
+ | < | ||
+ | def mirage := object: { | ||
+ | def foo() { 42 }; | ||
+ | } mirroredBy: TracingMirror; | ||
+ | </ | ||
- | When invoking the method '' | + | The AmbientTalk VM will then call '' |
+ | |||
+ | When invoking the method '' | ||
< | < | ||
- | > mirage.m(); | + | > mirage.foo(); |
- | invoked | + | invoked |
>> 42 | >> 42 | ||
</ | </ | ||
+ | |||
+ | The picture below gives an overview of the different objects involved in the actor. | ||
+ | |||
+ | {{: | ||
Whereas the example provided above may seem a little contrived, the reflective capabilities of AmbientTalk allow it to be extended with many abstraction relating to distributed computing for mobile ad hoc networks (AmbientTalk' | Whereas the example provided above may seem a little contrived, the reflective capabilities of AmbientTalk allow it to be extended with many abstraction relating to distributed computing for mobile ad hoc networks (AmbientTalk' |