Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Newbie: declarations
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Frank Atanassow <franka@c...>
Subject: Re: [Caml-list] Newbie: declarations
Sven LUTHER wrote (on 19-06-01 12:22 +0200):
> What about having a way to initialise struct types :
> 
> something like :
> 
> type datarec = {
>          mutable name : string = "?";
>          mutable color : string = "?";
>          mutable value : int = 0;
> }
> [...]
> But is it really usefull, i guess you could simply wrap the datatype in a
> constructor, and not worry about such things :

Yes, and it also doesn't make sense for the common case where fields are
polymorphic.

> 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

There is a simpler way:

  let new_datarec = { name = "?"; color = "?"; value = 0 }
  let x = { new_datarec with value = 113 }
  let y = { new datarec with color = "00FF00 FF00FF"; name = "y" }

-- 
Frank Atanassow, Information & Computing Sciences, Utrecht University
Padualaan 14, PO Box 80.089, 3508 TB Utrecht, Netherlands
Tel +31 (030) 253-3261 Fax +31 (030) 251-379
-------------------
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