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/04/27 14:47] – Added stijnm | at:tutorial:reflection [2008/11/06 16:29] – elisag | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | < | ||
====== Reflective Programming ====== | ====== Reflective Programming ====== | ||
- | Reflection is an integral part of the AmbientTalk programming language. Through the use of reflection, the core language can be extended with both programming support as well as new language constructs. Both examples require a different kind of reflective access. The introduction of programming support (e.g. to visualise AmbientTalk objects) relies on **introspection**, | + | [[wp> |
- | The reflective model of AmbientTalk is based on **mirrors**, meta-level objects which allow one to reflect on an objects | + | The reflective model of AmbientTalk is based on [[http:// |
===== Mirrors ===== | ===== Mirrors ===== | ||
- | As we have already | + | |
+ | 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() | + | > def mirrorOnP |
+ | >>< | ||
+ | |||
+ | > | ||
+ | >> | ||
</ | </ | ||
- | The code excerpt presented above uses the mirror to // | + | The code excerpt presented above uses the mirror to // |
- | In addition to allowing a program to reason about the structure of its objects, mirrors can also be used to write operations such as message sending in a first-class manner. The following example uses this power to invoke | + | {{: |
+ | |||
+ | 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 ===== | ||
- | 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); | + | |
} | } | ||
} | } | ||
Line 53: | Line 100: | ||
</ | </ | ||
- | This mirror | + | The primitive '' |
+ | |||
+ | < | ||
+ | def createTracingMirror(baseObject) { | ||
+ | mirror: { | ||
+ | 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 the method '' | + | In the code above, the closure passed to '' |
+ | |||
+ | {{: | ||
+ | |||
+ | |||
+ | When invoking the method '' | ||
+ | |||
+ | < | ||
+ | > mirage.m(); | ||
+ | invoked m on < | ||
+ | >> 42 | ||
+ | </ | ||
+ | |||
+ | Whereas the example provided above may seem a little contrived, the reflective capabilities of AmbientTalk allow it to be extended | ||
+ | |||
+ | ===== 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 Invocation Protocol** consists of methods to deal with both synchronous and asynchronous method invocation. 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 Marshalling 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** reifies | ||
+ | |||
+ | The **Object Instantiation Protocol** consists of the '' | ||
- | < | + | The **Relational Testing Protocol** consists |
- | Note that the use of '' | + | |
- | </ | + | |
- | Whereas | + | The **Type Tag 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 '' |
at/tutorial/reflection.txt · Last modified: 2010/11/16 16:32 by tvcutsem