When you define an operator such as
let (++) a b = a :: b
When you do
let v = foo a ++ bar b
bar is evaluated before foo. A workaround is to use let expression, i.e.
let e1 = foo a in let e2 = bar b in e1 ++ e2
However, sometimes it'd be handy to define an operator such as it's always evaluated from left to right. Is there a way to do it in OCaml or with a ppx or with lazy
?
The evaluation order of function arguments is part of the semantics of the language (it is currently not specified in OCaml), you cannot change it.
More often than not, whenever the order of evaluation of function argument starts to matter, it is a sign that there are too many loose global mutable states and that you want to tighten your grip on mutable states.
For instance, if you need to enforce a strict order of mutation on a global state, the common solution is to use a state monad, or a more eager variant like
This is essentially gives you the ability to define yourself how the state is evaluated in