Making Things Mutable
In ReasonML, things that we create with let
are immutable -- which means that we can't change them.
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.
Last updated