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:36] – tvcutsem | at:tutorial:reflection [2008/11/06 15:36] – elisag | ||
---|---|---|---|
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: | 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: | ||
Line 72: | Line 74: | ||
</ | </ | ||
- | 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 ===== | ||
- | 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); | + | |
- | }; | + | |
- | }; | + | |
} | } | ||
</ | </ | ||
- | < | + | The primitive '' |
- | This mirror | + | < |
+ | def createTracingMirror(baseObject) { | ||
+ | | ||
+ | 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 | + | In the code above, |
- | < | + | {{: |
- | Note that the use of '' | + | |
- | </note> | + | |
+ | When invoking | ||
+ | |||
+ | < | ||
+ | > mirage.m(); | ||
+ | invoked m on < | ||
+ | >> 42 | ||
+ | </code> | ||
- | 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' | + | 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 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 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 **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 '' | ||
Line 121: | Line 146: | ||
The **Slot Access and Modification Protocol** consists of operations which allow trapping both dynamic access and modification to slots. For instance, '' | 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 **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