caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Sven LUTHER <luther@dpt-info.u-strasbg.fr>
To: "Frédéric van der Plancke" <fvdp@decis.be>
Cc: caml <caml-list@inria.fr>
Subject: Re: [Caml-list] Newbie: declarations
Date: Tue, 19 Jun 2001 12:22:06 +0200	[thread overview]
Message-ID: <20010619122206.A24467@lambda.u-strasbg.fr> (raw)
In-Reply-To: <3B2F091F.EED457D9@decis.be>

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


  reply	other threads:[~2001-06-19 10:18 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-06-17  0:09 leary
2001-06-17 10:08 ` Vitaly Lugovsky
2001-06-18  2:19   ` leary
2001-06-18 15:18     ` Remi VANICAT
2001-06-18 20:19       ` [Caml-list] Good introduction for the working programmer in Objective CAML Mattias Waldau
2001-06-18 20:35         ` Miles Egan
2001-06-18 20:49         ` leary
2001-06-17 15:33 ` [Caml-list] Newbie: declarations David Fox
2001-06-18 14:52   ` FabienFleutot
2001-06-19  8:11 ` Frédéric van der Plancke
2001-06-19 10:22   ` Sven LUTHER [this message]
2001-06-19 12:25     ` Frank Atanassow

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20010619122206.A24467@lambda.u-strasbg.fr \
    --to=luther@dpt-info.u-strasbg.fr \
    --cc=caml-list@inria.fr \
    --cc=fvdp@decis.be \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).