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/23 15:52] – elisag | at:tutorial:reflection [2007/10/25 19:09] – slot tvcutsem | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | < | ||
- | |||
====== Reflective Programming ====== | ====== Reflective Programming ====== | ||
Line 8: | Line 6: | ||
===== 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 according to the dynamic call chain, the requesting object etc. The factory can be used implicitly using the '' | + | 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 baseObject | + | >def inspectable |
- | def field := nil; | + | |
- | def canonicalMethod() { nil }; | + | >>< |
- | def keyworded: | + | >def mirrorOnInspectable |
- | }; | + | >>< |
- | def mirror | + | > |
- | def slots := mirror.listSlots(); | + | >> |
- | slots.each: { | slot | system.println(slot) }; | + | > |
+ | >> | ||
+ | < | ||
+ | >def method | ||
+ | >>< | ||
+ | >method.bodyExpression | ||
+ | >> | ||
</ | </ | ||
- | The code excerpt presented above uses the mirror | + | Using a mirror on an object, it is possible to get access |
- | 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 addition to allowing a program to reason about the structure of its objects, mirrors can also be used to perform |
< | < | ||
- | def invokeUserMethod(object) { | + | >def isTestMethod(meth) { |
- | def userInput | + | (meth.name.text ~= " |
- | // This example assumes that the user typed a single symbol | + | { meth.parameters |
- | (reflect: | + | >>< |
- | }; | + | >def retainTestMethods(obj) { |
+ | (reflect: obj).listMethods() | ||
+ | | ||
+ | >>< | ||
+ | >def runTest(obj) { | ||
+ | | ||
+ | (reflect: | ||
+ | >>< | ||
+ | > | ||
+ | ok | ||
+ | >>nil | ||
</ | </ | ||
Line 80: | Line 106: | ||
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