Mutability

Mutability levels

The three levels of mutability are:

  • data, which is the default. These values don't change at all.
  • shared values can change in a thread-safe way.
  • mut values can change in any way.

A type which is not data must be marked as shared or mut.

r record mut x mut nat

That may seem redundant, but not all mutable types have mut fields.
A type is considered as mutable as the most mutable thing it references.
So, a type must be marked as mut if it references another mut type, even if it has no mut fields of its own.

To put it another way:

  • A mut type can only be referenced by other mut types.
  • A shared type can be referenced by shared or mut types.
  • data can be referenced anywhere.

What this gets you is the guarantee that a data type is immutable all the way down and has no hidden state anywhere.

Below, mut-points needs to be be marked mut, even though it has no mutable fields of its own.

mut-points record mut points mut-point[] mut-point record mut x mut float y mut float