title | description | lead | date | lastmod | draft | images | menu | weight | toc | ||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Concepts |
e2immu concepts. |
e2immu concepts |
2020-11-16 13:59:39 +0100 |
2023-10-24 16:04:39 +0100 |
false |
|
110 |
true |
The best place to start reading about the immutability concepts is:
There's also a slide deck covering the basic concepts.
-
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.