Why isn't mutable a subtype of immutable, or vice versa?
2 days ago
- Immutable and mutable data structures cannot be subtypes or supertypes of each other due to Liskov's substitution principle, which requires strict adherence to all contexts including implicit contracts.
- An immutable pair guarantees that repeated calls to car/cdr return the same value, enabling hash consing; a mutable pair lacks this guarantee, so they must be separate types.
- Ad hoc polymorphism (e.g., type classes, interfaces, duck typing) allows shared operations on immutable and mutable pairs without violating subtype relationships, but requires careful design.