* Order of evaluation when constructing record values
@ 2007-08-11 17:56 Jeff Meister
2007-08-11 18:15 ` Jeff Meister
` (2 more replies)
0 siblings, 3 replies; 6+ messages in thread
From: Jeff Meister @ 2007-08-11 17:56 UTC (permalink / raw)
To: caml-list
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.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Order of evaluation when constructing record values
2007-08-11 17:56 Order of evaluation when constructing record values 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
2 siblings, 2 replies; 6+ messages in thread
From: Jeff Meister @ 2007-08-11 18:15 UTC (permalink / raw)
To: caml-list
I seem to have answered my own question by playing around. It appears
that the order of evaluation is last-to-first and based on the order
of the fields in the type declaration, not in the value construction.
Oh well. I guess it's let-bindings for me.
On 8/11/07, Jeff Meister <nanaki@gmail.com> wrote:
> 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.
>
^ permalink raw reply [flat|nested] 6+ messages in thread
* RE: [Caml-list] Order of evaluation when constructing record values
2007-08-11 17:56 Order of evaluation when constructing record values Jeff Meister
2007-08-11 18:15 ` Jeff Meister
@ 2007-08-12 9:12 ` David Allsopp
2007-08-13 0:25 ` Jacques Garrigue
2 siblings, 0 replies; 6+ messages in thread
From: David Allsopp @ 2007-08-12 9:12 UTC (permalink / raw)
To: caml-list
> 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:
http://caml.inria.fr/pub/docs/manual-ocaml/expr.html - although the Record
section doesn't state that the order of evaluation is unspecified, the
section on Function application does: I think it's safe to assume that the
order of evaluation for record construction is similarly unspecified and so
could, in theory, change.
> 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.
Although it looks neat in this instance, in general relying on evaluation
order is risky and potentially unclear (which is one of the reasons that the
order of evaluation is left unspecified in the language specification).
> I seem to have answered my own question by playing around. It appears that
> the order of evaluation is last-to-first and based on the order of the
> fields in the type declaration, not in the value construction.
> Oh well. I guess it's let-bindings for me.
Exactly - so here, even a change in the type declaration could have broken
your code (even without a compiler change!).
See also http://tinyurl.com/ysm8te
David
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Caml-list] Order of evaluation when constructing record values
2007-08-11 17:56 Order of evaluation when constructing record values Jeff Meister
2007-08-11 18:15 ` Jeff Meister
2007-08-12 9:12 ` [Caml-list] " David Allsopp
@ 2007-08-13 0:25 ` Jacques Garrigue
2 siblings, 0 replies; 6+ messages in thread
From: Jacques Garrigue @ 2007-08-13 0:25 UTC (permalink / raw)
To: nanaki; +Cc: caml-list
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
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2007-08-14 0:54 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-08-11 17:56 Order of evaluation when constructing record values 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 is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox