TL;DR
The article explores why mutable and immutable data structures are not subtypes of each other, emphasizing the importance of Liskov's substitution principle in type systems.
✦ Why It Matters
Ensure your type system adheres to Liskov's principle to maintain type safety.
Key Takeaways
Full Summary
In programming, immutability refers to data structures that cannot be modified after creation, while mutability allows changes. The discussion often arises in programming forums about why these two variants are not treated as subtypes or supertypes.
According to Liskov's substitution principle, a subtype must be usable in any context where its supertype is expected, which is not the case for mutable and immutable types. If mutable were a subtype of immutable, it would violate this principle because mutable types can change state, while immutable types cannot.
This strict interpretation of subtyping ensures that type systems maintain their guarantees, preventing potential errors in code. Understanding this distinction is crucial for software engineers when designing type systems and data structures.
Related