caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Gerard Murphy" <gerard.murphy@sharp.co.uk>
To: <caml-list@inria.fr>
Subject: [Caml-list] Newbie question: semantics of 'mutable' in relation to deep / shallow copying of CAML values.
Date: Thu, 22 Nov 2001 12:25:04 -0000	[thread overview]
Message-ID: <11103C7901D5D411B45800508BB02D72012466E7@exchange1.sle.sharp.co.uk> (raw)

Hello,

As a *novice* CAML programmer, I've observed the following behaviour using
the OCaml interpreter:-

#        type example = { mutable label: string; mutable number: int };;

type example = { mutable label: string; mutable number: int };;


#       let value1 = { label = "Hello"; number = 0 };;
val value1 : example = {label="Hello"; number=0}

#       let value2 = value1;;
val value2 : example = {label="Hello"; number=0}

#       value1.label <- "I have been mutated!";;
- : unit = ()


#       value2;;
val value2 : example = {label="I have been mutated!"; number=0}

Surprise! 'value2' is sharing state with 'value1', but neither 'value1' nor
'value2' are
CAML references.

OK, in one sense I expect this behaviour - as long as your dealing with the
applicative part of the
language, it should make no difference as to whether a value is actually
copied, or whether it is
simply referred to in two different places. In terms of the low-level
representation, the value is either
literally copied around or a pointer to the value is copied and the value
garbage collected in some manner.

(Looking at the notes on the CAML to C interface, I believe integers are
handled by the former
approach, everything else by the latter.)

So, it seems reasonable in terms of the implementation that if one copy of
the value is mutated, the
original will change too.

My question is: is this part of the expected semantics? I've seen no mention
of this in the OCaml
manual or FAQ. If it is part of the language definition, I presume that an
implementation is safe
to assert that record values with mutable fields have to be copied 'by
reference' as opposed to 'by value'?

Thanks in advance,

Gerard Murphy









-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


             reply	other threads:[~2001-11-22 12:28 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-11-22 12:25 Gerard Murphy [this message]
2001-11-22 13:11 ` Daniel de Rauglaudre
2001-11-22 14:23 ` Alain Frisch

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=11103C7901D5D411B45800508BB02D72012466E7@exchange1.sle.sharp.co.uk \
    --to=gerard.murphy@sharp.co.uk \
    --cc=caml-list@inria.fr \
    /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).