In ReasonML, things that we create with
let are immutable — which means that we can’t change them.

let num = 5;

Once num is bound to 5 it is stuck with that value for the duration
of it’s scope.

ReasonML doesn’t completely restrict us to immutability though. The
ref construct allows us to bind a variable to a sort of box that holds a
value. We can then look in the box and change what is in the box.

We use ref to bind our variable to a box with some initial value. The :=
assignment operator allows us to change what’s in the box. Anytime we want
to refer to what’s in the box, we postfix our variable with ^.

Also of note: while list instances are not mutable, array instances are.

Today I Learned is an open-source project by
Hashrocket
that exists to catalogue the sharing & accumulation of knowledge
as it happens day-to-day. Posts have a 200-word limit, and
posting is open to any Rocketeer as well as selected friends of
Hashrocket. We hope you enjoy learning along with us.