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 NAA01949; Thu, 22 Nov 2001 13:28:17 +0100 (MET) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id NAA02251 for ; Thu, 22 Nov 2001 13:28:16 +0100 (MET) Received: from inca.sle.sharp.co.uk ([193.133.140.101]) by concorde.inria.fr (8.11.1/8.10.0) with ESMTP id fAMCSFT25783 for ; Thu, 22 Nov 2001 13:28:15 +0100 (MET) Received: from SACPQDPRO1GGM ([192.16.16.116]) by inca.sle.sharp.co.uk (8.8.8+Sun/8.8.8) with SMTP id MAA22984 for ; Thu, 22 Nov 2001 12:20:55 GMT Reply-To: From: "Gerard Murphy" To: 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 Message-ID: <11103C7901D5D411B45800508BB02D72012466E7@exchange1.sle.sharp.co.uk> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook CWS, Build 9.0.2416 (9.0.2910.0) Importance: Normal X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4133.2400 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk 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