User Tools

Site Tools


at:tutorial:objects

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
at:tutorial:objects [2007/07/10 12:46] – fixed tvcutsemat:tutorial:objects [2007/07/10 22:14] – changed tvcutsem
Line 120: Line 120:
 This cloning semantics reinforces the semantics of **IS-A** as promoting a unique link between a parent and a child object. **IS-A** delegation most closely corresponds to class-based inheritance. This cloning semantics reinforces the semantics of **IS-A** as promoting a unique link between a parent and a child object. **IS-A** delegation most closely corresponds to class-based inheritance.
  
-===== Delegation and dynamic inheritance ===== +===== Delegation and Dynamic Inheritance ===== 
-The parent of an object is bound to field named ''super''. The delegation chain defined by an object and its parent (or chain of parents) determines the scope in which the message is looked up. As any field in AmbientTalk objects, the ''super'' field can be dynamically modified.+ 
 +In AmbientTalk, all objects delegate messages they cannot understand to the object stored in their field named ''super''. The delegation chain defined by an object and its parent (or chain of parents) determines the scope in which message is looked up. For ex-nihilo created objects, like the ''Point'' object defined previously, the ''super'' slot is by default set to ''nil''. When a message is finally delegated all the way up to ''nil'', ''nil'' informs the original receiver of the message of the failed lookup, which by default reports the error by means of a ''SelectorNotFound'' exception. 
 + 
 +Because ''super'' is a regular field of an AmbientTalk object (it is just installed by default), it can be dynamically modified, giving rise to //dynamic inheritance//: the ability of objects to change their object inheritance hierarchy at runtimeThe SELF language has demonstrated how this language feature can be put to good use for modeling stateful objects. For example: 
 + 
 +<code> 
 +def openConnection := object: { 
 +  def send(msg) { ... }; 
 +}; 
 +def closedConnection := object: { 
 +  def send(msg) { ... }; 
 +}; 
 +def connection := object: { 
 +  def init() { 
 +    super := closedConnection; 
 +  }; 
 +  def open() { 
 +    super := openConnection; 
 +  }; 
 +  def close() { 
 +    super := closedConnection; 
 +  }; 
 +
 +</code> 
 + 
 +In the above example, the ''connection'' object can be in an "open" or "closed" state. Rather than implementing the state-specific behaviour of e.g. the ''send'' method by means of a state variable and an ''if''-test, state-specific methods are implemented in dedicated objects which are dynamically assigned as the parent of the ''connection'' object when it changes state. When ''connection.send(msg)'' is evaluated, the ''send'' message is delegated to the parent, resulting in the application of the method in the correct state. 
 + 
 +<note> 
 +In AmbientTalk, ''self'' and ''super'' indicate the receiver object and the parent respectively. However, ''self'' is a pseudovariable: it is a special keyword, not a variable that can be assigned to. ''super'' on the other hand, denotes a regular field in an object. This is a significant difference with respect to many other object-oriented languages who also treat ''super'' as a special keyword. There is, however, a reason for this special treatment of ''super'': "super-sends". The repercussions of AmbientTalk's treatment of ''super'' on "super-sends" is discussed below. 
 +</note> 
 + 
 +===== First-class Delegation ===== 
 + 
 +AmbientTalk provides a special message-sending operator ''^'' (the "caret" or "hat" symbol) to express the //explicit// delegation of a message to an object. The code below illustrates the use of the ''^'' operator:
  
 <code> <code>
-def openConnection := object: {...}; +def Enumerable := object: { 
-def closedConnection := object: {...}; +  def collectclosure 
-def connection := object{ +    def := cloneself; 
-    def open() +    self.each: |v| 
-      super := openConnection.new();+      c.add(closure(v))
     };     };
-    def close() { +  }; 
-      super := closedConnection.new();+}; 
 +def Array := object: { 
 +  def elements := []; 
 +  def init() { ... }; 
 +  def collectclosure { 
 +    Enumerable^collect: closure; 
 +  }; 
 +  def each: clo { 
 +    1.to: elements.length do: { |i| 
 +      clo(elements[i])
     };     };
-  }+  }
 +};
 </code> </code>
  
-<note important> +A message sent to an object using the ''^'' symbol (e.g. to the ''Enumerable'' object in the example above) will start the method lookup in this object and execute the method body with the ''self'' pseudovariable **left unchanged** to the message sender. In the code example abovewhen ''collect:'' is invoked on an ''Array'' object, the array object //delegates// the message to the ''Enumerable'' object. As such, method lookup starts in ''Enumerable'', finds the method there, and then invokes it with ''self'' left bound to the ''Array'' object. Hence, the ''self.each:'' send in the ''Enumerable'' object uses ''Array'''s definition of ''each:'' to generate the elements in the collection.
-In AmbientTalk, ''self'' and ''super'' indicate the current object and its parent respectivelyWhile the former corresponds to a language keyword the latter is just a field name of the object. +
-</note>+
  
-===== First-class delegation ===== +Of course, the example above is bit contrived: we could have just assigned ''Enumerable'' as the parent of ''Array'' such that we would not even have to write the "delegating" ''collect:'' method in ''Array''. However, what the explicit ''^'' delegation operator allows is the expression of patterns resembling //multiple inheritance// where some requests are delegated to one object, while other methods can be delegated to other objects. Explicit delegation enables the expression of delegation patterns which would be awkward or difficult to express using only single (delegation-based) inheritance.
-AmbientTalk provides special message-sending operator ''^'' (the "caret" or "hat" symbol) to express the //explicit// delegation of a message to an object. The code below illustrates the use of the ''^'' operator in the implementation of the ''init'' method of the ''point3D'' object.+
  
 <code> <code>
-def point3D := extend: point with: {+def Point3D := extend: Point with: {
   def z := 0;   def z := 0;
   def init(aX, aY, aZ) {   def init(aX, aY, aZ) {
Line 150: Line 190:
     z := aZ;     z := aZ;
   };   };
-}+};
 </code> </code>
  
-A message sent to an object using the ''^'' symbol (e.g. to the parent object in the example above) will start the method lookup in this object (and its parents) and then execute the method body with the ''self'' pseudovariable bound to the message sender. 
  
 <note warning> <note warning>
at/tutorial/objects.txt · Last modified: 2013/05/17 20:23 by tvcutsem