This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
at:tutorial:reflection [2007/10/08 13:31] 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 already mentioned in the introduction, | ||
- | |||
- | < | ||
- | >def mirrorOnOne := reflect: 1; | ||
- | >>< | ||
- | > | ||
- | >> | ||
- | < | ||
- | </ | ||
- | |||
- | The code excerpt presented above uses the mirror to // | ||
- | |||
- | When reflecting upon a user-defined object, we can observe that every object has some implictly defined methods and fields, in addition to those which are defined when constructing the object. Every AmbientTalk object has a '' | ||
- | |||
- | < | ||
- | >def inspectable := object: { | ||
- | def map(arg1, @restArgs) { restArgs.map(arg1); | ||
- | >>< | ||
- | >def mirrorOnInspectable := reflect: inspectable; | ||
- | >>< | ||
- | > | ||
- | >> | ||
- | > | ||
- | >> | ||
- | < | ||
- | >def method := mirrorOnInspectable.grabMethod(`map); | ||
- | >>< | ||
- | > | ||
- | >> | ||
- | </ | ||
- | |||
- | Using a mirror on an object, it is possible to get access to a representation of the object' | ||
- | |||
- | 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) { | ||
- | | ||
- | { meth.parameters == [] } }; | ||
- | >>< | ||
- | >def retainTestMethods(obj) { | ||
- | | ||
- | | ||
- | >>< | ||
- | >def runTest(obj) { | ||
- | | ||
- | | ||
- | >>< | ||
- | > | ||
- | ok | ||
- | >>nil | ||
- | </ | ||
- | |||
- | This part of the tutorial has provided a basic feeling of how AmbientTalk' | ||
- | |||
- | ===== 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 access and modification to slots. These operations are further refined based on whether they transitively search the dynamic or lexical parent chain. For instance, for the lookup of a variable, '' | ||
- | |||
- | 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 '' |