at:tutorial:multiparadigm
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
at:tutorial:multiparadigm [2007/06/19 14:44] – * tvcutsem | at:tutorial:multiparadigm [2011/06/07 18:29] (current) – *minor tvcutsem | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== On Scoping, Closures, Methods and Messages ====== | ====== On Scoping, Closures, Methods and Messages ====== | ||
- | This tutorial chapter goes into a bit more detail on the subtle | + | This tutorial chapter goes into a bit more detail on the interplay between AmbientTalk' |
===== Lexical Scope vs Object Scope ===== | ===== Lexical Scope vs Object Scope ===== | ||
Line 13: | Line 13: | ||
- Qualified access to a variable, e.g. '' | - Qualified access to a variable, e.g. '' | ||
- | These rules also hold for method invocation: the invocation '' | + | These rules also hold for method invocation: the invocation '' |
- | Probably the most important | + | The most important |
< | < | ||
Line 48: | Line 48: | ||
<note important> | <note important> | ||
- | For many object-oriented programmers, | + | For many object-oriented programmers, |
+ | |||
+ | The confusion stems from the fact that many OO languages -- like Java -- make no distinction between both access forms. | ||
</ | </ | ||
Line 67: | Line 69: | ||
It is important that '' | It is important that '' | ||
- | ==== Nesting Objects ==== | + | ===== Nesting Objects |
AmbientTalk exploits its lexical scoping rules to the fullest extent by allowing as much program elements as possible to be nested. For example, it is possible to lexically nest objects within other objects, or even to nest functions within other functions. In this section, we describe how nested objects interact with the scoping rules presented above. | AmbientTalk exploits its lexical scoping rules to the fullest extent by allowing as much program elements as possible to be nested. For example, it is possible to lexically nest objects within other objects, or even to nest functions within other functions. In this section, we describe how nested objects interact with the scoping rules presented above. | ||
- | === Facets === | + | ==== Facets |
One of the most appealing use cases for nesting objects is that it allows a very secure kind of //sharing// between objects: all objects that are nested within another object have the privilege of all sharing the same lexical scope. This form of sharing is much more secure that sharing data via delegation, because the set of objects sharing the scope is statically fixed. In the E language, such nested objects are called [[http:// | One of the most appealing use cases for nesting objects is that it allows a very secure kind of //sharing// between objects: all objects that are nested within another object have the privilege of all sharing the same lexical scope. This form of sharing is much more secure that sharing data via delegation, because the set of objects sharing the scope is statically fixed. In the E language, such nested objects are called [[http:// | ||
Line 97: | Line 99: | ||
The '' | The '' | ||
- | === Nesting and Delegation === | + | ==== Nesting and Delegation |
Thanks to AmbientTalk' | Thanks to AmbientTalk' | ||
Line 129: | Line 131: | ||
In the example above, writing '' | In the example above, writing '' | ||
- | ==== Methods vs Closures ==== | + | ===== Methods vs Closures |
As mentioned previously, AmbientTalk not only allows objects to be nested within other objects, but also allows functions to be nested within other functions. At this point, it becomes important to distinguish between // | As mentioned previously, AmbientTalk not only allows objects to be nested within other objects, but also allows functions to be nested within other functions. At this point, it becomes important to distinguish between // | ||
Line 152: | Line 154: | ||
In the above example, '' | In the above example, '' | ||
- | === Top-level Functions === | + | ==== Top-level Functions |
Top-level functions are actually methods in AmbientTalk. This is because all top-level code is treated as the initialization code of that file's module object. Hence, it is legal to use '' | Top-level functions are actually methods in AmbientTalk. This is because all top-level code is treated as the initialization code of that file's module object. Hence, it is legal to use '' | ||
- | === Block closures === | + | ==== Block closures |
Block closures, as created by means of the syntax '' | Block closures, as created by means of the syntax '' | ||
- | ==== External Methods ==== | + | ===== External Methods |
- | It is possible to define methods externally on an object. For example: | + | It is possible to define methods |
< | < | ||
Line 177: | Line 179: | ||
<note important> | <note important> | ||
- | There is one more difference between closures and external methods: next to the special treatment of '' | + | There is one more difference between closures and external methods: next to the special treatment of '' |
The rationale behind this decision is that '' | The rationale behind this decision is that '' | ||
+ | </ | ||
+ | |||
+ | <note warning> | ||
+ | External methods, while powerful, introduce many subtle issues in the language. Therefore, this feature may disappear from later releases of AmbientTalk. It is therefore wise to stay clear from using this feature. For an overview of the problem introduced by external field or method declarations, | ||
</ | </ | ||
Line 186: | Line 192: | ||
In AmbientTalk, | In AmbientTalk, | ||
- | Methods become first-class when they are selected from existing objects, as if they were fields. For example, it is possible to write: | + | Methods become first-class when they are selected from existing objects. |
< | < | ||
Line 193: | Line 199: | ||
}; | }; | ||
- | def squareClo := Math.square; | + | def squareClo := Math.□ |
> squareClo(2) | > squareClo(2) | ||
>> 4 | >> 4 | ||
</ | </ | ||
- | When a field access results in a method | + | Slot selection automatically lifts a method slot into a closure. The closure can subsequently be passed around as a stand-alone value. When the method closure is invoked, the original method is ran with '' |
< | < | ||
Line 210: | Line 216: | ||
}; | }; | ||
}; | }; | ||
- | > [1, | + | > [1, |
>> [2,3,4] | >> [2,3,4] | ||
</ | </ | ||
The '' | The '' | ||
+ | |||
+ | The selection operator is also required when accessing lexically visible methods as first-class closures: | ||
+ | |||
+ | < | ||
+ | def o := object: { | ||
+ | def m() { ... }; | ||
+ | def grabTheMethod() { &m }; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Selection using ''&'' | ||
+ | |||
+ | < | ||
+ | def o := object: { | ||
+ | x := 5; | ||
+ | }; | ||
+ | def f := o.&x; | ||
+ | > f | ||
+ | >> <native closure: | ||
+ | > o.x := 6; f() | ||
+ | >> 6 | ||
+ | </ | ||
+ | |||
+ | In the same vein, one may select a mutator method for a field '' | ||
===== First-class Messages ===== | ===== First-class Messages ===== |
at/tutorial/multiparadigm.1182257073.txt.gz · Last modified: 2007/06/25 21:13 (external edit)