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 [2007/07/18 16:32] – jorge | at:tutorial:reflection [2008/09/15 17:39] – tvcutsem | ||
---|---|---|---|
Line 3: | Line 3: | ||
[[wp> | [[wp> | ||
- | The reflective model of AmbientTalk is based on [[http:// | + | The reflective model of AmbientTalk is based on [[http:// |
===== Mirrors ===== | ===== Mirrors ===== | ||
- | AmbientTalk uses a mirror-based architecture to provide reflective access to its objects. The basic principle of a mirror-based architecture is that all reflective facilities are encapsulated in a mirror object which provides reflective access to precisely one object, its reflectee. Moreover, the mirror of the object is not directly accessible as a slot of the object. Instead, a separate factory must be used to create mirrors, which allows the program to hand out different mirrors | + | |
+ | As we have mentioned in the introduction, | ||
+ | |||
+ | A convenience primitive exists that allows AmbientTalk programmers to acquire a mirror on an object without explicitly having to consult the mirror | ||
+ | |||
+ | Once a mirror has been created, it can be used to inspect | ||
< | < | ||
- | def baseObject | + | def Point := object: { |
- | def field := nil; | + | def x := 0; |
- | def canonicalMethod() { nil }; | + | def y := 0; |
- | def keyworded: arg1 method: arg2 { nil }; | + | def distanceToOrigin() |
}; | }; | ||
- | def mirror := reflect: | + | def p := Point.new(2, |
- | def slots := mirror.listSlots(); | + | // request a mirror |
- | slots.each: { | slot | system.println(slot) }; | + | > def mirrorOnP |
+ | >>< | ||
+ | |||
+ | > | ||
+ | >> | ||
</ | </ | ||
- | The code excerpt presented above uses the mirror to // | + | The code excerpt presented above uses the mirror to // |
- | In addition | + | 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 |
< | < | ||
- | def invokeUserMethod(object) { | + | // let's add a z coordinate to our point |
- | def userInput | + | def [zaccessor, zmutator] |
- | // This example assumes that the user typed a single symbol | + | // we only add the accessor, so the slot is read-only |
- | (reflect: object).invoke(object, userInput, []); | + | mirrorOnP.addSlot(zaccessor); |
- | }; | + | // let's test it: |
+ | > p.z | ||
+ | >> 0 | ||
+ | // we can also read slots reflectively: | ||
+ | > def x : | ||
+ | >> < | ||
+ | > x() | ||
+ | >> 2 | ||
+ | // and we can also invoke methods reflectively: | ||
+ | > mirrorOnP.invoke(p, lobby.at.lang.values.createInvocation(`distanceToOrigin, | ||
+ | >> 3.605551275463989 | ||
+ | // finally, we can remove slots... | ||
+ | > mirrorOnP.removeSlot(`z); | ||
</ | </ | ||
- | This part of the tutorial has provided a basic feeling of how AmbientTalk' | + | The following example contains the core of a unit testing framework by showing how to select all zero-argument methods of an object whose name starts with '' |
+ | |||
+ | < | ||
+ | >def isTestMethod(meth) { | ||
+ | | ||
+ | { meth.parameters.length == 0 } }; | ||
+ | >>< | ||
+ | >def retainTestMethods(obj) { | ||
+ | | ||
+ | | ||
+ | >>< | ||
+ | >def runTest(obj) { | ||
+ | | ||
+ | | ||
+ | >>< | ||
+ | > | ||
+ | ok | ||
+ | >> | ||
+ | </ | ||
+ | |||
+ | 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 ===== | ||
Line 51: | Line 99: | ||
} | } | ||
</ | </ | ||
+ | |||
+ | < | ||
This mirror overrides the default implementation of the meta-operation '' | This mirror overrides the default implementation of the meta-operation '' | ||
Line 76: | Line 126: | ||
The **Object Passing Protocol** consists of two methods '' | The **Object Passing Protocol** consists of two methods '' | ||
- | The **Slot Access and Modification Protocol** consists of operations which allow trapping both access and modification to slots. These operations are further refined based on whether they transitively search the dynamic or lexical parent chain. For instance, | + | The **Slot Access and Modification Protocol** consists of operations which allow trapping both dynamic |
- | 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** 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 **Instantiation Protocol** consists of the '' | The **Instantiation Protocol** consists of the '' |
at/tutorial/reflection.txt · Last modified: 2010/11/16 16:32 by tvcutsem