Mutable Bindings
Quick overview: Refs
Refs allow mutable bindings in your program. They are a thin wrapper around a record with a mutable field called contents
.
type ref('a) = { mutable contents: 'a, };
There is syntax built into the language to work with ref
structures.
ref(value)
creates a ref with contents asvalue
x^
accesses the contents of the refx
x :=
updates the contents of the refx
let x = ref(10); x := x^ + 10; x := x^ + 3; /* x^ is 23 */
If you prefer to avoid the ref
syntax the following code is exactly equivalent to the syntax above:
let x = {contents: 10}; x.contents = x.contents + 10; x.contents = x.contents + 3; /* x.contents is 23 */
Note: Make sure you need mutable bindings before using them. The trivial example above should not use mutable bindings and instead should use: Binding Shadowing