Urgent.News

What's breaking now, across thousands of outlets.

Tech

FAQ: Why isn’t mutable a subtype of immutable, or vice versa?

The question of whether mutable data structures can be subtypes of immutable ones or vice versa has been a topic of debate in programming language discussions. While it might seem logical, it is not formally correct and can lead to the loss of type checking guarantees. This is due to the strict interpretation of Liskov's substitution principle, which requires that a subtype can be used in every context where a supertype is expected.

To illustrate, consider a simple pair data structure. Immutable pairs have specific operations like car and cdr that provide a consistent contract, allowing for safe computations such as hash consing. Conversely, mutable pairs can be modified, meaning the contract for these operations is not guaranteed to remain the same.

When mutable pairs are used in place of immutable ones, the absence of certain operations can lead to type errors. Similarly, using immutable pairs where mutable pairs are expected can also result in errors because the explicit contract of immutability is violated. Therefore, mutable and immutable pairs must be treated as distinct types with their unique sets of operations, ensuring type safety and maintaining the integrity of the language's type checking mechanisms.

Written by urgent.news from Lobsters's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at crumbles.blog →

More in Tech

Who helped Samsung become chip giant

Through the 1980s, Japan pushed the United States — the birthplace and longtime leader of the semiconductor industry — out of the world memory chip market, and the damage ran deep enough that Intel…

More from Friday 18 September →