at:tutorial:reflection
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
at:tutorial:reflection [2008/09/15 17:26] – * tvcutsem | 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 28: | Line 29: | ||
</ | </ | ||
- | The code excerpt presented above uses the mirror to // | + | The code excerpt presented above uses the mirror to // |
+ | |||
+ | {{: | ||
+ | |||
+ | The code excerpt below shows how one can add and remove slots to and from an object, and how one can explicitly access values and invoke methods upon an object, reflectively: | ||
< | < | ||
- | >def inspectable | + | // let's add a z coordinate to our point |
- | def map(arg1, @restArgs) { restArgs.map(arg1); } }; | + | def [zaccessor, zmutator] |
- | >>< | + | // we only add the accessor, so the slot is read-only |
- | >def mirrorOnInspectable := reflect: inspectable; | + | mirrorOnP.addSlot(zaccessor); |
- | >>< | + | // let's test it: |
- | >mirrorOnInspectable.listFields() | + | > p.z |
- | >>[<field:super>] | + | >> |
- | >mirrorOnInspectable.listMethods() | + | // we can also read slots reflectively: |
- | >>[<method:map>, < | + | > def x :=mirrorOnP.grabSlot(`x) |
- | < | + | >> <accessor |
- | >def method := mirrorOnInspectable.grabMethod(`map); | + | // and we can also invoke methods reflectively: |
- | >>< | + | > mirrorOnP.invoke(p, lobby.at.lang.values.createInvocation(`distanceToOrigin, |
- | >method.bodyExpression | + | >> |
- | >> | + | // finally, we can remove slots... |
+ | > mirrorOnP.removeSlot(`z); | ||
</ | </ | ||
- | Using a mirror on an object, it is possible to get access to a representation of the object' | + | The following example contains |
- | + | ||
- | In addition to allowing a program to reason about the structure of its objects, mirrors can also be used to perform operations such as method invocation in a first-class manner. The following example shows how to select all zero-argument methods whose name starts with '' | + | |
< | < | ||
>def isTestMethod(meth) { | >def isTestMethod(meth) { | ||
| | ||
- | { meth.parameters == [] } }; | + | { meth.parameters.length |
>>< | >>< | ||
>def retainTestMethods(obj) { | >def retainTestMethods(obj) { | ||
Line 62: | Line 66: | ||
>def runTest(obj) { | >def runTest(obj) { | ||
| | ||
- | | + | |
>>< | >>< | ||
> | > | ||
Line 69: | Line 73: | ||
</ | </ | ||
- | This part of the tutorial has provided a basic feeling of how AmbientTalk' | + | This part of the tutorial has provided a basic feeling of how AmbientTalk' |
+ | |||
+ | < | ||
+ | defaultMirror.listSlots.map: | ||
+ | </ | ||
+ | |||
+ | |||
+ | A complete overview of all meta-operations will be presented | ||
===== 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 | ||
< | < | ||
- | def dynamicExtensionMirror | + | def createTracingMirror(baseObject) { |
- | def doesNotUnderstand(selector) { | + | extend: defaultMirror.new(baseObject) with: { |
- | system.println(" | + | def invoke(slf, invocation) { |
- | system.println(" | + | system.println(" |
- | def input := system.readln(); | + | super^invoke(slf, invocation); |
- | if: !( "" | + | } |
- | def definition := read: input; | + | } |
- | eval: definition in: base; | + | |
- | } else: { | + | |
- | super^doesNotUnderstand(selector); | + | |
- | }; | + | |
- | }; | + | |
} | } | ||
</ | </ | ||
- | <note> **TODO** Add a word on **base** </note> | + | The primitive '' |
+ | |||
+ | <code> | ||
+ | def TracingMirror := mirror: { | ||
+ | def invoke(slf, invocation) { | ||
+ | system.println(" | ||
+ | super^invoke(slf, | ||
+ | } | ||
+ | } | ||
+ | </code> | ||
- | This mirror overrides the default implementation of the meta-operation '' | + | The next step is to create objects whose method calls can be traced. These objects |
< | < | ||
def mirage := object: { | def mirage := object: { | ||
- | def m() { self.x | + | def foo() { 42 }; |
- | } mirroredBy: | + | } mirroredBy:{ |emptyBase| createTracingMirror(emptyBase) }; |
</ | </ | ||
- | When invoking | + | In the code above, |
- | < | + | Another alternative |
- | Note that the use of '' | + | |
- | </ | + | |
- | 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' | + | < |
+ | def mirage := object: { | ||
+ | def foo() { 42 }; | ||
+ | } mirroredBy: TracingMirror; | ||
+ | </ | ||
+ | |||
+ | The AmbientTalk VM will then call '' | ||
+ | |||
+ | When invoking the method '' | ||
+ | |||
+ | < | ||
+ | > mirage.foo(); | ||
+ | invoked foo on < | ||
+ | >> 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' | ||
===== The Metaobject Protocol ===== | ===== The Metaobject Protocol ===== | ||
- | The Meta-Object Protocol of AmbientTalk can be divided into a series of independent protocols. Whereas the full semantics and signature of the meta-methods can be found in the [[http://prog.vub.ac.be/ | + | The Meta-Object Protocol of AmbientTalk can be divided into a series of independent protocols. Whereas the full semantics and signature of the meta-methods can be found in the [[http://soft.vub.ac.be/ |
+ | |||
+ | The **Message Invocation Protocol** consists of methods to deal with both synchronous and asynchronous method invocation. It provides necessary hooks to intercept both the reception of asynchronous messages and the invocation of synchronous messages. Moreover, it provides a hook to intercept asynchronous messages being sent by the object, allowing the object to add additional metadata to the message. The '' | ||
- | The **Message Passing Protocol** consists of methods to deal with both synchronous and asynchronous message sending. It provides necessary hooks to intercept both the reception of asynchronous messages and the invocation of synchronous messages. Moreover, it provides a hook to intercept asynchronous messages being sent by the object, allowing the object to add additional metadata to the message. The '' | + | {{: |
- | The **Object | + | The **Object |
The **Slot Access and Modification Protocol** consists of operations which allow trapping both dynamic access and modification to slots. For instance, '' | The **Slot Access and Modification Protocol** consists of operations which allow trapping both dynamic access and modification to slots. For instance, '' | ||
- | The **Structural Access Protocol** consists of operations used list all available slots, get access to a first-class slot representation and to add new slots to an existing object. The '' | + | The **Structural Access Protocol** |
- | The **Instantiation Protocol** consists of the '' | + | The **Object |
The **Relational Testing Protocol** consists of the methods '' | The **Relational Testing Protocol** consists of the methods '' | ||
- | The **Type | + | The **Type |
The **Evaluation Protocol** ensures that any AmbientTalk object can be part of a parse tree, and therefore every object provides meaningful implementations of the '' | The **Evaluation Protocol** ensures that any AmbientTalk object can be part of a parse tree, and therefore every object provides meaningful implementations of the '' |
at/tutorial/reflection.txt · Last modified: 2010/11/16 16:32 by tvcutsem