title: Are default values to optional parameters evaluated on every function call?
body: <p>The <a href="http://caml.inria.fr/pub/docs/manual-ocaml/lablexamples.html#sec46" rel="nofollow noreferrer">OCaml spec</a> documents the use of default values to optional parameters, but doesn't specify what happens if the default values are e.g. a function that could return a different value each time.</p>
  
  <p>From some testing, it seems like the value is evaluated each time <em>if</em> the argument is not passed in; e.g.</p>
  
  <pre class="lang-ml prettyprint-override"><code>let x = ref 0;;
  
  let incr x =
    x := !x + 1;
    !x
  ;;
  
  let test ?(a = incr x) () = a;;
  </code></pre>
  
  <p>calling <code>test ()</code> results in 1, 2, 3, ..., incrementing each time, but calling <code>test ~a:123 ()</code> doesn't increment the ref.</p>
  
  <p>It seems like the behavior of running <code>test ()</code> or <code>test ~a ()</code> is roughly something like</p>
  
  <pre class="lang-ml prettyprint-override"><code>let test ?a () =
    let a =
      match a with
      | None -&gt; incr x
      | Some a -&gt; a
    in
    body_of_test ~a ()
  ;;
  </code></pre>
  
  <p>Is this the right way to model this? And also, is this behavior documented somewhere?</p>