This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
at:tutorial:reflection [2008/09/15 17:41] tvcutsem |
at:tutorial:reflection [2010/11/16 16:32] tvcutsem |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Reflective Programming ====== | ||
- | [[wp> | ||
- | |||
- | The reflective model of AmbientTalk is based on [[http:// | ||
- | |||
- | ===== 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 factory (the primitive does so in the programmer' | ||
- | |||
- | Once a mirror has been created, it can be used to inspect an object as a collection of so-called //slot// objects, objects which bind a name to a value (a method slot is simply a slot that binds a name to a method object). | ||
- | |||
- | < | ||
- | def Point := object: { | ||
- | def x := 0; | ||
- | def y := 0; | ||
- | def distanceToOrigin() { (x*x + y*y).sqrt }; | ||
- | }; | ||
- | def p := Point.new(2, | ||
- | // request a mirror on p via the mirror factory | ||
- | > def mirrorOnP := reflect: p; | ||
- | >>< | ||
- | |||
- | > | ||
- | >> | ||
- | </ | ||
- | |||
- | 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: | ||
- | |||
- | < | ||
- | // let's add a z coordinate to our point | ||
- | def [zaccessor, zmutator] := lobby.at.lang.values.createFieldSlot(`z, | ||
- | // we only add the accessor, so the slot is read-only | ||
- | 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, | ||
- | >> 3.605551275463989 | ||
- | // finally, we can remove slots... | ||
- | > mirrorOnP.removeSlot(`z); | ||
- | </ | ||
- | |||
- | 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 | ||
- | >>nil | ||
- | </ | ||
- | |||
- | 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 near the end of this chapter. | ||
- | |||
- | ===== 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 could define objects which allow for the dynamic addition of unknown methods and fields. First of all, we need to create a mirror instance which we can use to create new objects from. This can be performed using the '' | ||
- | |||
- | < | ||
- | def dynamicExtensionMirror := mirror: { | ||
- | def doesNotUnderstand(selector) { | ||
- | system.println(" | ||
- | system.println(" | ||
- | def input := system.readln(); | ||
- | if: !( "" | ||
- | def definition := read: input; | ||
- | eval: definition in: base; | ||
- | } else: { | ||
- | super^doesNotUnderstand(selector); | ||
- | }; | ||
- | }; | ||
- | } | ||
- | </ | ||
- | |||
- | < | ||
- | |||
- | This mirror overrides the default implementation of the meta-operation '' | ||
- | |||
- | < | ||
- | def mirage := object: { | ||
- | def m() { self.x }; | ||
- | } mirroredBy: dynamicExtensionMirror; | ||
- | </ | ||
- | |||
- | When invoking the method '' | ||
- | |||
- | < | ||
- | 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' | ||
- | |||
- | ===== 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:// | ||
- | |||
- | 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 Passing Protocol** consists of two methods '' | ||
- | |||
- | 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 **Instantiation Protocol** consists of the '' | ||
- | |||
- | The **Relational Testing Protocol** consists of the methods '' | ||
- | |||
- | The **Type Testing Protocol** consists of the methods '' | ||
- | |||
- | The **Evaluation Protocol** ensures that any AmbientTalk object can be part of a parse tree, and therefore every object provides meaningful implementations of the '' |