caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jacques Garrigue <garrigue@math.nagoya-u.ac.jp>
To: nanaki@gmail.com
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Order of evaluation when constructing record values
Date: Mon, 13 Aug 2007 09:25:28 +0900 (JST)	[thread overview]
Message-ID: <20070813.092528.214236695.garrigue@math.nagoya-u.ac.jp> (raw)
In-Reply-To: <6806cf750708111056v19107705p288e0b0bb152ef63@mail.gmail.com>

From: "Jeff Meister" <nanaki@gmail.com>

> I ask for forgiveness in advance for this silly pedantic question.
> 
> I think my question is best illustrated with an example. Say I have
> this simple record type for holding a date:
> 
> type date = { year : int; month : int; day : int; }
> 
> Now, I want to read the year/month/day values from stdin, and I know
> they will appear in that order. So I do the following:
> 
> let today = {
>   year = read_int ();
>   month = read_int ();
>   day = read_int ();
> }
> 
> For this to work, I need the ints to be read in the order given, or I
> could end up with a day of 2007 and a year of 11. Is there any
> guarantee that OCaml will follow that order of evaluation when
> constructing the record? Or do I have to force it with let-bindings
> like this:
> 
> let today =
>   let y = read_int () in
>   let m = read_int () in
>   let d = read_int () in
>   { year = y; month = m; day = d; }
> 
> Of course, it's not that big a deal for me to just use the let-binding
> method, but I'm curious, and it might make my code look nicer if I can
> rely on order of evaluation.

You definitely have to use let-bindings.
The order of evaluation without let-bindings is left unspecified in
the reference manual.
IIRC, in practice this should be right-to-left, following the
definition of the datatype (not the order in the function!), but this
does not seem a good idea to rely on that.

Jacques Garrigue


      parent reply	other threads:[~2007-08-13  7:12 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-08-11 17:56 Jeff Meister
2007-08-11 18:15 ` Jeff Meister
2007-08-11 22:13   ` [Caml-list] " Jon Harrop
2007-08-12  8:25   ` Till Varoquaux
2007-08-12  9:12 ` [Caml-list] " David Allsopp
2007-08-13  0:25 ` Jacques Garrigue [this message]

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=20070813.092528.214236695.garrigue@math.nagoya-u.ac.jp \
    --to=garrigue@math.nagoya-u.ac.jp \
    --cc=caml-list@yquem.inria.fr \
    --cc=nanaki@gmail.com \
    /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).