Version française
Home     About     Download     Resources     Contact us    
Browse thread
Order of evaluation when constructing record values
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: David Allsopp <dra-news@m...>
Subject: RE: [Caml-list] Order of evaluation when constructing record values
> 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