Concepts
e2immu concepts
The best place to start reading about the immutability concepts is:
There’s also a slide deck covering the basic concepts.
TL;DR
-
modification: changes to the object graph of an object’s fields.
-
container: a type which does not make modifications to the arguments of its constructors and non-private methods. You can use it as safe storage for your objects.
-
final fields: all fields are effectively final (either explicitly, or only modified during the construction phase). Java
record
types are a nice example. -
independence: a modification to one object has no effect on the other object. Fields of implicitly immutable type cannot be dependent, because the type has no means of modifying them.
-
a type is immutable when the following four criteria are met:
- the type is final fields;
- its fields are not modified;
- its fields are either private, or of immutable type;
- constructor parameters are independent of the fields, as are the return values and parameters of non-private methods
-
eventually immutable: immutability is achieved after a field changes from a before state into an after or final state. This blocks a number of modifying methods, which makes the type effectively immutable.
Primitives, Object
, and String
are immutable - once we ignore the
synchronization methods of Object
.
Unmodifiable variants of collection classes, such as the result of Set.of()
and List.copyOf()
, are immutable as well.
The project proposes simple support classes such as SetOnce
and EventuallyFinal
that help propagate eventual immutability throughout your program.