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 22:21] – changed tvcutsemat:tutorial:objects [2007/07/10 22:29] – * tvcutsem
Line 202: Line 202:
  
 ===== Encapsulation ===== ===== Encapsulation =====
-In AmbientTalk, all fields and methods are "public" via selectionStill, a field or method can be made "private" by means of lexical scoping. The following code shows the definition of an object inside the definition of a function. The fields and methods of this object cannot be accessed directly from outside the funuction.+ 
 +AmbientTalk has no notion of "visibility modifiers" for fields or methods. All fields and methods of an object are considered "public"Nevertheless, a field or method can be made "private" to a scope by means of lexical scoping (a technique sometimes referred to as [[http://www.erights.org/elib/capability/ode/ode-objects.html|lambda abstraction]]). The following code shows the definition of an object inside the definition of a function. Although all of the object'fields and methods are public, the object can make use of lexically visible fields and methods of outer objects or functions which are not simply accessible "from the outside":
  
 <code> <code>
-def makeObject(hidden) { +def makeBankAccount(balance) { 
-    object: { +  object: { 
-      def foo() { /* use hidden */ } +    def deposit(amnt) { 
-    }+      balance := balance + amnt; 
 +      "ok" 
 +    };
   }   }
 +}
 </code> </code>
  
-Due to the encapsulation of this object the following instruction fails:+Because the bank account object encapsulates its ''balance'' in its private, lexical scope, the following code fails:
  
 <code> <code>
-makeObject(5).hidden+makeBankAccount(100).balance
->>Lookup failure : selector hidden could not be found in +>>Lookup failure : selector balance could not be found in 
   <object:5068254>   <object:5068254>
 </code> </code>
at/tutorial/objects.txt · Last modified: 2013/05/17 20:23 by tvcutsem