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 MAA04322; Tue, 19 Jun 2001 12:18:28 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id MAA04229 for ; Tue, 19 Jun 2001 12:18:27 +0200 (MET DST) Received: from dpt-info.u-strasbg.fr (dpt-info.u-strasbg.fr [130.79.6.1]) by nez-perce.inria.fr (8.11.1/8.10.0) with ESMTP id f5JAIQP19778 for ; Tue, 19 Jun 2001 12:18:26 +0200 (MET DST) Received: from lambda.u-strasbg.fr (mail@lambda.u-strasbg.fr [130.79.90.63]) by dpt-info.u-strasbg.fr (8.9.3/8.9.3) with ESMTP id MAA01349; Tue, 19 Jun 2001 12:17:37 +0200 Received: from luther by lambda.u-strasbg.fr with local (Exim 3.22 #1 (Debian)) id 15CIe6-0006My-00; Tue, 19 Jun 2001 12:22:06 +0200 Date: Tue, 19 Jun 2001 12:22:06 +0200 To: =?iso-8859-1?Q?Fr=E9d=E9ric?= van der Plancke Cc: caml Subject: Re: [Caml-list] Newbie: declarations Message-ID: <20010619122206.A24467@lambda.u-strasbg.fr> References: <20010616170909.B10922@jean> <3B2F091F.EED457D9@decis.be> Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <3B2F091F.EED457D9@decis.be> User-Agent: Mutt/1.3.18i From: Sven LUTHER Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On Tue, Jun 19, 2001 at 10:11:11AM +0200, Frédéric van der Plancke wrote: > [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 = "!" > What about having a way to initialise struct types : something like : type datarec = { mutable name : string = "?"; mutable color : string = "?"; mutable value : int = 0; } And then you could do : let x : datarec = { value = 113 } or maybe let x = { type datarec with value = 113 } or something such ? This would permit to do as above, but without the sharing problem. But is it really usefull, i guess you could simply wrap the datatype in a constructor, and not worry about such things : type datarec = { mutable name : string; mutable color : string; mutable value : int; } let new_datarec n c v = { name = match n with None -> "?" | Some n -> n; color = match c with None -> "?" | Some c -> c; value = match v with None -> 0 | Some v -> v; } Then you could simply do : let x = new_datarec None None (Some 113) let y = new_datarec (Some "y") ("00FF00 FF00FF") None Or even something more cleaner using labels and optional values. Friendly, Sven Luther ------------------- 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