Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Newbie question: semantics of 'mutable' in relation to deep / shallow copying of CAML values.
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Gerard Murphy <gerard.murphy@s...>
Subject: [Caml-list] Newbie question: semantics of 'mutable' in relation to deep / shallow copying of CAML values.
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