[LLVMdev] [Caml-list] Ocaml(opt) & llvm

Alain Frisch alain.frisch at lexifi.com
Fri Nov 30 00:58:43 PST 2007


Gordon Henriksen wrote:
> (1) Mutable variables exist only in the heap: let x = 43 ref is  
> literally shorthand for let x = { mutable contents = 43 }, which  
> allocates a single-cell object on the heap.

This is not always true. When the reference is only dereferenced or
assigned locally, it is treated as a local mutable variable. Example:

let f () =
   let x = ref 0 in
   for i = 1 to 10 do incr x done;
   !x

Of course, if the reference is passed to another function, it will
indeed be heap allocated.

-- Alain





More information about the llvm-dev mailing list