This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
at:tutorial:multiparadigm [2007/06/19 11:21] tvcutsem *added |
at:tutorial:multiparadigm [2011/06/07 18:29] tvcutsem *minor |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== On Scoping, Closures, Methods and Messages ====== | ||
- | < | ||
- | |||
- | This tutorial chapter goes into a bit more detail on the subtle interplay between AmbientTalk' | ||
- | |||
- | ===== Lexical Scope vs Object Scope ===== | ||
- | |||
- | AmbientTalk distinguishes between two kinds of scopes: | ||
- | - the **lexical scope**, which is the set of all variables that are lexically visible in the program text. In other words: all variables in an enclosing scope are part of the lexical scope of the enclosed (nested) scope. | ||
- | - the **object scope**, which is delimited by a chain of delegating objects. When sending a message to an object, the object and its parent objects delimit the scope in which the message is looked up. | ||
- | |||
- | The rules for distinguishing which scope to use when resolving an identifier are straightforward: | ||
- | - Unqualified access to a variable, e.g. '' | ||
- | - Qualified access to a variable, e.g. '' | ||
- | |||
- | These rules also hold for method invocation: the invocation '' | ||
- | |||
- | Probably the most important conseqence of these rules is that great care has to be taken when an object accesses its own fields or methods. It can now do so in two ways. For example: | ||
- | |||
- | < | ||
- | def o := object: { | ||
- | def x := 5; | ||
- | def getStatic() { x }; | ||
- | def getDynamic() { self.x }; | ||
- | } | ||
- | </ | ||
- | |||
- | In the code snippet above, '' | ||
- | |||
- | < | ||
- | def o2 := extend: o with: { | ||
- | def x := 6; | ||
- | } | ||
- | </ | ||
- | |||
- | This program behaves as follows: | ||
- | < | ||
- | > | ||
- | >> 5 | ||
- | > | ||
- | >> 5 | ||
- | > | ||
- | >> 5 | ||
- | > | ||
- | >> 6 | ||
- | </ | ||
- | |||
- | ==== Nesting Objects ==== | ||
- | |||
- | ==== Methods vs Closures ==== | ||
- | |||
- | ==== External Methods ==== | ||
- | |||
- | ===== First-class Methods ===== | ||
- | |||
- | ===== First-class Messages ===== |