OCaml supports typed references, which refer to memory cells on the heap. The type rules for references are:
mutable
Wait, what does {contents="astringref"}
mean?
OCaml implements references via mutable
fields in records:
Only fields marked mutable
support mutation:
type mixed = { no : bool; mutable maybe : bool }
let r = { no = false; maybe = true};;
r.maybe <- false;; (* OK *)
r.no <- true;; (* type error *)
Note that mutation (<-
) has type unit
, whereas functional update of records (using with
) produces a new value:
type 'a union_cell = { v : 'a ;
mutable p : 'a union_cell option;
mutable sz : int }
let make_set v = { v = v; p = None; sz=1}
Some data structures with internal references are impossible to construct without assignment (in OCaml):
cs2041.org