caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Newbie question: semantics of 'mutable' in relation to deep / shallow copying of CAML values.
@ 2001-11-22 12:25 Gerard Murphy
  2001-11-22 13:11 ` Daniel de Rauglaudre
  2001-11-22 14:23 ` Alain Frisch
  0 siblings, 2 replies; 3+ messages in thread
From: Gerard Murphy @ 2001-11-22 12:25 UTC (permalink / raw)
  To: caml-list

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


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2001-11-22 14:23 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-11-22 12:25 [Caml-list] Newbie question: semantics of 'mutable' in relation to deep / shallow copying of CAML values Gerard Murphy
2001-11-22 13:11 ` Daniel de Rauglaudre
2001-11-22 14:23 ` Alain Frisch

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).