at:tutorial:reflection
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
at:tutorial:reflection [2008/09/15 17:26] – * tvcutsem | at:tutorial:reflection [2008/09/15 17:58] – rewritten tvcutsem | ||
---|---|---|---|
Line 28: | Line 28: | ||
</ | </ | ||
- | 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 | + | mirrorOnP.addSlot(zaccessor); |
- | >>< | + | // let's test it: |
- | > | + | > p.z |
- | >>[<field:super>] | + | >> |
- | >mirrorOnInspectable.listMethods() | + | // we can also read slots reflectively: |
- | >>[< | + | > def x :=mirrorOnP.grabSlot(`x) |
- | < | + | >> <accessor method for:x> |
- | >def method := mirrorOnInspectable.grabMethod(`map); | + | > x() |
- | >>< | + | >> |
- | >method.bodyExpression | + | // and we can also invoke methods reflectively: |
- | >> | + | > mirrorOnP.invoke(p, lobby.at.lang.values.createInvocation(`distanceToOrigin, |
+ | >> | ||
+ | // 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 65: | ||
>def runTest(obj) { | >def runTest(obj) { | ||
| | ||
- | | + | |
>>< | >>< | ||
> | > | ||
Line 69: | Line 72: | ||
</ | </ | ||
- | 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 involves adding objects which have a different implementation for some of the default meta-operations. In this part of the tutorial, we describe how a programmer | + | |
+ | Extending the AmbientTalk core language involves adding objects which have a different implementation for some of the default meta-operations. In this part of the tutorial, we describe how a programmer | ||
+ | |||
+ | 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 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); | + | |
- | }; | + | |
- | }; | + | |
} | } | ||
</ | </ | ||
- | < | + | The primitive '' |
- | This mirror | + | < |
+ | def createTracingMirror(baseObject) { | ||
+ | | ||
+ | def invoke(slf, invocation) { | ||
+ | system.println(" | ||
+ | super^invoke(slf, | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | The next step is to create 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, |
- | < | + | When invoking |
- | Note that the use of '' | + | |
- | </note> | + | < |
+ | > mirage.m(); | ||
+ | invoked m on < | ||
+ | >> 42 | ||
+ | </code> | ||
- | 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' |
===== The Metaobject Protocol ===== | ===== The Metaobject Protocol ===== |
at/tutorial/reflection.txt · Last modified: 2010/11/16 16:32 by tvcutsem