Browse thread
Order of evaluation when constructing record values
-
Jeff Meister
- Jeff Meister
- David Allsopp
- Jacques Garrigue
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: | 2007-08-11 (18:15) |
From: | Jeff Meister <nanaki@g...> |
Subject: | Re: Order of evaluation when constructing record values |
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. >