Java's JDK provides unmodifiable collections but lacks a proper ImmutableList type in the type system. The post explains why adding one is fundamentally difficult: ImmutableList and List cannot extend each other because immutability is a positive guarantee, not merely the absence of mutation. A proper hierarchy would require an intermediate UnmodifiableList supertype, similar to Scala's approach. However, retrofitting such types into the existing Java ecosystem is practically impossible due to bytecode incompatibility — changing method signatures would require recompiling the entire Java ecosystem. Guava's ImmutableList is useful locally but too easy to bypass to serve as a compiler-enforced guarantee.

10m read timeFrom nipafx.dev
Post cover image
Table of contents
▚ What's An Immutable Collection?▚ Just Add Immutable Collections, Already!▚ Immutability Is A Feature▚ Can Unmodifiable And Immutable Collections Be Retrofitted?▚ Reflection

Sort: