Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] let or val in objects
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: John Prevost <visigoth@c...>
Subject: Re: [Caml-list] let or val in objects
On Sun, Mar 31, 2002 at 10:40:51PM +0200, Richard Nyberg wrote:
>Hello, I'm a bit confused regarding let bindings and nonmutable vals in
>objects. Are there any difference between the classes a and b? or are they
>equivalent?
>
>Like this:
>
>class a fd =
>  let is = in_channel_of_descr fd in object ... end
>
>class b fd =
>  object val is = in_channel_of_descr fd ... end;;

In the second case, "is" is a named value that's part of the object--inheriting
classes can access its value, and if it is mutable, change the value.

In the first case, "is" is a variable in the closure of the methods in
the object.  Inheriting classes may not access its value in any way, including
(of course) modifying it if it has a mutable component.

Using lets outside of the object defintion can be very useful when you wish
to have private values which inheriting classes may not access.  (For example,
in order to preserve some invariant.)  However, you may note that a
let-bound variable (if it is a function) may not itself access the contents
of the object.

So they are fairly significantly different.  :)  The normal use is to
let-bind something like an input_stream, which your methods can access but
inheriting methods cannot.  Then inheriting objects have no choice but to
use your accessor functions to access the stream.

John.
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners