The Dart VM has a concept of deeply immutable instances.
Deeply immutable instances can be shared across isolates within the same group.
A deeply immutable type is a type for which all instances that have this type are deeply immutable.
This is useful for static checks on classes annotated @pragma('vm:deeply-immutable')
. All the instance fields of such classes must have a deeply immutable type.
A list of immutable types:
bool
double
int
Null
String
Float32x4
Float64x2
Int32x4
Pointer
@pragma('vm:deeply-immutable')
In addition to instances from deeply immutable types, instances can also be deeply immutable while their type is not deeply immutable:
SendPort
(implemented externally package:isolate
, so cannot be final
https://github.com/dart-lang/sdk/issues/54885#issuecomment-1967329435)Capability
(has SendPort
as subtype so cannot be final
)RegExp
(can be implemented externally, not final
)StackTrace
(can be implemented externally, not final
)Type
(can be implemented externally, not final
)This means users cannot mark classes with fields typed with these types as @pragma('vm:deeply-immutable')
.
The VM also has shallow immutability.
The UntaggedObject::ImmutableBit
tracks whether an instance is deeply or shallowly immutable at runtime. For shallow immutable objects, the VM needs to know the layout and what to check when to check for to check deep immutability at runtime.
The Class::is_deeply_immutable
tracks whether all instances of a class are deeply immutable.
This bit can be set in two ways:
vm:deeply-immutable
pragma.The vm:deeply-immutable
pragma is added to classes of which their type is deeply immutable.
This puts the following restrictions on these classes:
final
or sealed
. This ensures no non-deeply-immutable subtypes are added by external code.These restructions are enforced by DeeplyImmutableValidator.