18 Mutable Fields

We can make fields in records mutable. Let's define points with colors, where the color is mutable.

Note that mutable goes before the field name, it's not part of the type (i.e. it's not a mutable string or anything).

Now let's make a point

And let's update the color

Now the field in point p has been updated to be "white" instead of "red". We can't do this to the other fields:

Notice that the mutation operator for mutable fields is not the same as the one for refs. We can actually implement refs ourselves!

The type is declared in the standard library (they're actually compiled to external functions, but they behave very similarly, see here)