From: "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 10:11:11 +0200 [thread overview]
Message-ID: <3B2F091F.EED457D9@decis.be> (raw)
In-Reply-To: <20010616170909.B10922@jean>
[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
next prev parent reply other threads:[~2001-06-19 8:15 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 [this message]
2001-06-19 10:22 ` Sven LUTHER
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=3B2F091F.EED457D9@decis.be \
--to=fvdp@decis.be \
--cc=caml-list@inria.fr \
/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