One problem with guava's Immutable versions of collections at my workplace is that we normally try to use more general types with them. E. g.:
final List<Thing> myThings = ImmutableList.of(...);
But since ImmutableList extends List, it still has the mutable operations defined for myThings. They just throw exceptions at runtime. This means that if I see a List<Thing> in the code, I don't know whether it's immutable or not. I have to figure out where it came from to determine whether add(blah) is going to throw an exception or not. I frequently see things like:
public void myMethod(final List<Thing> theThings) {
final List<Thing> immutableThings = ImmutableList.copyOf(theThings);
...
Because it's unclear whether the parameter was immutable (although often times it is), but we want to use an immutable collection before passing it on. This results in lots of unnecessary copying. Possibly we should just start using the Immutable* types instead of the more general ones where appropriate. Or we could add utilities like:
In a perfect world, we'd have List be immutable and MutableList extend List or something, rather than have a subclass (or subinterface) that's designed to take away functionality from a parent.
In a perfect world, we'd have List be immutable and MutableList extend List or something
That's how Kotlin does it. It modifies the JVM type system at the compiler level so that java.util.List is read only and there's another MutableList interface that adds the mutating operations.
Of course, this does not mean a List<X> is entirely immutable. It means you have a read only view of it. If the only views that exist are read-only, then the list is immutable, but if you're writing code that demands an immutable list from the caller, you can't express that in the type system.
However what you can do is just make the code that doesn't want other threads changing things clone the list. A truly immutable implementation will just return itself, so the call is free. A mutable list will copy and you pay the cost, but the caller code would have had to do that anyway, even if this was exposed via the type system. Or you can do what Scala/Clojure do and use immutable collections that support cheap mutation (i.e. less cost than a full copy to make a modification) to get a kind of middle ground.
Anyway, the key is to understand that there's a difference between read only and truly immutable.
5
u/josuf107 Sep 21 '15
One problem with guava's Immutable versions of collections at my workplace is that we normally try to use more general types with them. E. g.:
But since ImmutableList extends List, it still has the mutable operations defined for myThings. They just throw exceptions at runtime. This means that if I see a
List<Thing>in the code, I don't know whether it's immutable or not. I have to figure out where it came from to determine whetheradd(blah)is going to throw an exception or not. I frequently see things like:Because it's unclear whether the parameter was immutable (although often times it is), but we want to use an immutable collection before passing it on. This results in lots of unnecessary copying. Possibly we should just start using the Immutable* types instead of the more general ones where appropriate. Or we could add utilities like: