From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id XAA19832; Mon, 1 Apr 2002 23:06:55 +0200 (MET DST) Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id XAA05025 for caml-list@pauillac.inria.fr; Mon, 1 Apr 2002 23:06:54 +0200 (MET DST) Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id LAA09100 for ; Mon, 1 Apr 2002 11:21:22 +0200 (MET DST) Received: from laurelin.dementia.org ([208.167.88.73]) by nez-perce.inria.fr (8.11.1/8.11.1) with ESMTP id g319LLf16454 for ; Mon, 1 Apr 2002 11:21:21 +0200 (MET DST) Received: (from jprevost@localhost) by laurelin.dementia.org (8.11.6/8.11.6) id g319SLK75015; Mon, 1 Apr 2002 04:28:21 -0500 (EST) (envelope-from visigoth@cs.cmu.edu) X-Authentication-Warning: laurelin.dementia.org: jprevost set sender to visigoth@cs.cmu.edu using -f Date: Mon, 1 Apr 2002 04:28:21 -0500 From: John Prevost To: Richard Nyberg , caml-list@inria.fr Subject: Re: [Caml-list] let or val in objects Message-ID: <20020401042820.A74928@laurelin.dementia.org> References: <20020331224051.A5546@gromit.it.su.se> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: <20020331224051.A5546@gromit.it.su.se> User-Agent: Mutt/1.3.22.1i Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk 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