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 revisionBoth sides next revision
at:tutorial:objects [2007/07/09 21:03] – * tvcutsemat:tutorial:objects [2007/07/09 21:13] – rewrote tvcutsem
Line 45: Line 45:
  
 ===== Cloning and instantiation ===== ===== Cloning and instantiation =====
-As said before in this section, AmbientTalk objects are created [[objects#Objects,_fields_and_methods|ex-nihilo]] or by cloning and adapting an existing object. The code below shows the instatiation of a new ''point'' object by using the cloning semantics.+As noted above, AmbientTalk objects are created [[#Objects,_fields_and_methods|ex-nihilo]] or by cloning and adapting an existing object. The code below shows the instatiation of a new point object by //instantiating// the ''Point'' prototype:
  
 <code> <code>
-def anotherPoint := point.new(2,3)+def anotherPoint := Point.new(2,3)
 </code> </code>
  
-Every object understands the message ''new'', which creates a clone (a shallow copy) of the receiver object and initializes the clone by invoking its ''init'' method with the arguments that were passed to new (''aX'' and ''aY'' in the example of the ''point'' object). Hence, the ''init'' method plays the role of constructor” for AmbientTalk objects. AmbientTalk’s object instantiation protocol closely corresponds to class instantiation in class-based languagesexcept that the new object is a clone of an existing objectrather than an empty object allocated from a class.+Every object understands the message ''new'', which creates a clone (a shallow copy) of the receiver object and initializes the clone by invoking its ''init'' method with the arguments that were passed to new (''aX'' and ''aY'' in the example code). Hence, the ''init'' method plays the role of "constructorfor AmbientTalk objects. Hencein the code above''anotherPoint'' shares its methods with its ''Point'' prototype, but it has its own set of fields:
  
-AmbientTalk also provides a ''clone'' language contsruct which only creates a clone of the receiver object without calling the ''init'' method (as a matter of fact the ''new'' message desribed above does nothing more but invoking this construct and the ''init'' method subsequently).+<code> 
 +> anotherPoint.x 
 +>> 2 
 +> Point.x 
 +>> 0 
 +> anotherPoint.x := 3 
 +>> nil 
 +> Point.x 
 +>> 0 
 +</code> 
 + 
 +<note> 
 +AmbientTalk's object instantiation protocol closely corresponds to class instantiation in class-based languages. The major difference lies in the evaluation context of the ''init'' method: in a class-based language, the constructor is ran in the context of an empty object, freshly allocated from the class blueprint. In AmbientTalk, the ''init'' method is ran in the context of a shallow copy of an original object. Hence, in the ''init'' method, fields do not necessarily contain ''nil'' values: they have the value of the clonee. This can sometimes be useful to express the state of a clone as a delta w.r.t. the state of its clonee. 
 +</note> 
 + 
 +AmbientTalk also provides a ''clone:'' construct which only creates a clone of the receiver object without calling the ''init'' method ((As a matter of fact the ''new'' message desribed above does nothing more but invoking this construct and the ''init'' method subsequently.)).
  
 <code> <code>
-def clonedPoint := clone: point+def clonedPoint := clone: Point 
 +> clonedPoint.x 
 +>> 0 
 +> clonedPoint.x := 2 
 +>> nil 
 +> Point.x 
 +>> 0
 </code> </code>
  
at/tutorial/objects.txt · Last modified: 2013/05/17 20:23 by tvcutsem