Skip to content

Immutability

Spotifier edited this page Mar 8, 2019 · 1 revision

Most objects in Mobius are required to be immutable. Immutability imposes a constraint on what your objects can do, therefore making them easier to reason about. The Mobius framework is in fact based on the principle that Model, Event, and Effect objects are immutable – it wouldn’t be possible to have Mobius without this constraint.

It is important to be aware of the distinction between value and reference types in Swift. structs, enums, and tuples are value types which keep local copies of their data. classes are reference types, meaning several references can exist to the same instance. This potentially allows models, event, or effects to be mutated without Mobius being aware of it. For this reason, we recommend using value types. We also recommend using let for all fields in these types and transitively in any types you reference. While this is not strictly necessary for immutability when using value types, we feel it helps convey your intent clearly and makes it significantly easier to transition to using reference types if you need to do so for performance reasons. Consumers of your model should not need to be aware of your choice of value or reference semantics.

Clone this wiki locally