Mutability
Mutability levels
The three levels of mutability are:
data, which is the default. These values don't change at all.sharedvalues can change in a thread-safe way.mutvalues can change in any way.
A type which is not data must be marked as shared or mut.
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
muttype can only be referenced by othermuttypes. - A
sharedtype can be referenced bysharedormuttypes. datacan 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.