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 KAA32283; Tue, 19 Jun 2001 10:15:47 +0200 (MET DST) 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 KAA32395 for ; Tue, 19 Jun 2001 10:15:45 +0200 (MET DST) Received: from decis.be ([194.78.208.93]) by concorde.inria.fr (8.11.1/8.10.0) with ESMTP id f5J8FiX06284 for ; Tue, 19 Jun 2001 10:15:44 +0200 (MET DST) Received: from decis.be [192.168.0.7] by decis.be [194.78.208.93] with SMTP (MDaemon.v3.5.3.R) for ; Tue, 19 Jun 2001 10:11:01 +0200 Message-ID: <3B2F091F.EED457D9@decis.be> Date: Tue, 19 Jun 2001 10:11:11 +0200 From: =?iso-8859-1?Q?Fr=E9d=E9ric?= van der Plancke X-Mailer: Mozilla 4.6 [en] (WinNT; I) X-Accept-Language: en,fr,nl MIME-Version: 1.0 CC: caml Subject: Re: [Caml-list] Newbie: declarations References: <20010616170909.B10922@jean> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-MDRemoteIP: 192.168.0.7 X-Return-Path: fvdp@decis.be X-MDaemon-Deliver-To: caml-list@inria.fr Reply-To: fvdp@decis.be Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk [Excuse-me if this is a duplicate. I haven't seen the previous instance of this message on the list yet, I think I sent it to the wrong address. This message contains additional thoughts anyway ;-)] leary@nwlink.com wrote: > More to the point, is there a way to declare a record variable without > listing/initializing all the fields? If yes, what are the default values? You can create a default record and initialize new records from a copy of the default record like this: type datarec = { mutable name : string; mutable color : string; mutable value : int; } let default_datarec = { name = "?"; color = "?"; value = 0; } let x = { default_datarec with value = 113 } let y = { default_datarec with name = "y"; color = "00FF00 FF00FF" } Beware of aliasing though: the name string is mutable and is shared by all instances for which no specific name was given, so: # x.name.[0] <- '!';; - : unit = () # default_datarec.name;; - : string = "!" Furthermore "someone" may directly modify the default_datarec in your back. This is not the safest way to do it. Unless you do this: let default_datarec () = { name = String.copy "?"; (* Need to copy in order to avoid aliasing *) color = String.copy "?"; value = 0; } let x = let d = default_datarec () in { d with value = 113 } One can't write { default_datarec () with value = 113 } ... And BTW, I like Python's immutable strings ! (Wouldn't be long to implement an immutable-strings module in OCaml though.) /Frederic vdP ------------------- 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