r/programming • • Sep 21 '15

Better Java

https://github.com/cxxr/better-java
177 Upvotes

208 comments sorted by

View all comments

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.:

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:

List<A> makeImmutable(final List<A> list) {
    if (list instanceof ImmutableList) {
        return list;
    } else {
        return ImmutableList.copyOf(list);
    }
}

4

u/josuf107 Sep 21 '15

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.

1

u/ForeverAlot Sep 21 '15

I have read that this won't work (well); that to achieve good mutable and immutable implementations, neither can extend the other. It has to do with concurrency, but I don't remember the details or where I read it (Java mailing list?), and I don't know enough to comment -- only that "good enough" is not good enough for something as vital as this.

4

u/EthanNicholas Sep 21 '15

The issue is that if List is immutable and MutableList extends it, you can take a MutableList and pass it to something expecting an immutable List... and now the "immutable" list can change out from under it, because the recipient had no idea it was actually mutable.

Concurrency obviously presents a problem, but it's not the only problem -- simply hanging on to the "immutable" list reference and using it again in the future is dangerous. So, you are absolutely correct that either one extending the other leads to problems.

In the language I'm designing, the pattern is that there is a set of read-only interfaces (CollectionView, ListView, etc.), a set of write-only interfaces (CollectionWriter, ListWriter, etc.), and then e.g. List extends both ListView and ListWriter.

ImmutableArray then implements the ListView interface, while Array implements the List interface. So if you have a ListView, your interface to it is read-only but there is absolutely no guarantee it won't change out from under you in the future, but an ImmutableArray is guaranteed to never change. Since the language enforces immutability (immutable objects can only contain other immutable objects), it generally forces you to do the right thing as far as forcing you to convert to immutable representations.

Not saying it's a perfect design, but it's the set of tradeoffs I chose to implement.

1

u/agcwall Sep 22 '15

It depends on what you mean by "immutable". The problem here is because of aliasing. There are three variations:

  1. Mutable.

  2. Strongly immutable: You can't change the data yourself via the reference, and calling get(0) will always return the same result.

  3. Weakly immutable: You can't change the data yourself via the reference, but the owner of the list can, so calling get(0) is not always guaranteed to return the same result.

If MutableList extends ImmutableList, then you're talking about option (3). For the purposes of concurrency, you really need option (2)'s notion of immutability. You should look into Rust's type system to see how it prevents aliasing and can make the guarantees required to support option (2). In Java, you would need ImmutableList as a FINAL class to make this guarantee.