ref
s and mutable
record fields
A record field can be designated as mutable
. This allows the <-
operator to be used, which modifies the value that f
is pointing to. More formally:
The expression
expr1 . field <- expr2
evaluatesexpr1
to a record value, which is then modified in-place by replacing the value associated tofield
in this record by the value ofexpr2
. This operation is permitted only iffield
has been declared mutable in the definition of the record type.1
type foo = { mutable alpha: int; beta: string };;
let f = { alpha = 0; beta = "abc" } in
f.alpha <- 5;
f;;
type bar = { delta: int; gamma: string };;
let b = { delta = 0; gamma = "abc" } in
{ b with delta = 5 };;
The ref
type (short for “references”) is essentially syntactic sugar for a record with a single mutable field.2
#show_type ref;;
(* type 'a ref = { mutable contents : 'a; } *)
#show (!);;
(* ( ! ) : 'a ref -> 'a = "%field0" *)
(* Equivalent to: let ( ! ) r = r.contents *)
#show (:=);;
(* ( := ) : 'a ref -> 'a -> unit = "%setfield0" *)
(* Equivalent to: let ( := ) r x = r.contents <- x *)