caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: John Prevost <visigoth@cs.cmu.edu>
To: Richard Nyberg <rnyberg@it.su.se>, caml-list@inria.fr
Subject: Re: [Caml-list] let or val in objects
Date: Mon, 1 Apr 2002 04:28:21 -0500	[thread overview]
Message-ID: <20020401042820.A74928@laurelin.dementia.org> (raw)
In-Reply-To: <20020331224051.A5546@gromit.it.su.se>

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


  reply	other threads:[~2002-04-01 21:06 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-03-31 20:40 Richard Nyberg
2002-04-01  9:28 ` John Prevost [this message]
2002-04-01 22:54   ` james woodyatt
2002-04-02  4:08 ` Brian Rogoff

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20020401042820.A74928@laurelin.dementia.org \
    --to=visigoth@cs.cmu.edu \
    --cc=caml-list@inria.fr \
    --cc=rnyberg@it.su.se \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).