Tuple and function execution order

Hello Andrei,

The order of evaluation of sub-expressions making up a tuple expression is not specified:

http://caml.inria.fr/pub/docs/manual-ocaml/expr.html#sec151

Most of the time, it does not matter, but typically, if the sub-expressions do side effects, you have to explicit the order of evaluation to avoid “surprises”. So you can rewrite your piece of code like this to get what you expected:

let () = 
  let s1 = read_line () in
  let s2 = read_line () in
  Printf.printf "%s\n%s\n" s1 s2

Note that the order of evaluation of expressions making up a function application is unspecified too:

http://caml.inria.fr/pub/docs/manual-ocaml/expr.html#sec138

4 Likes