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:25] – updated tvcutsem | at:tutorial:reflection [2008/09/15 17:41] – tvcutsem | ||
---|---|---|---|
Line 22: | Line 22: | ||
// request a mirror on p via the mirror factory | // request a mirror on p via the mirror factory | ||
> def mirrorOnP := reflect: p; | > def mirrorOnP := reflect: p; | ||
- | >>< | + | >>< |
> | > | ||
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 ===== |
at/tutorial/reflection.txt · Last modified: 2010/11/16 16:32 by tvcutsem