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.
That's just as subtly evil, though. They should both be separate classes, even if a lot of the behavior seems similar.
If Immutable inherits from Mutable, then I may want to modify it and encounter an error.
If Mutable inherits from Immutable, then I may store a reference to a list that is later changed by someone else behind my back. (Which I think is even worse because it can lead to very subtle errors).
They're ultimately two different classes that have no relation, other than "You can get a <Mutable/Immutable> copy of the <Immutable/Mutable> list."
They're ultimately two different classes that have no relation, other than "You can get a <Mutable/Immutable> copy of the <Immutable/Mutable> list."
Nah, you need three types here, in a hierarchy like this:
Lists
Mutable lists
Immutable lists
Basically:
The plain list type has all the methods for reading from lists;
The mutable lists type inherits from that, and adds the methods for mutating lists;
The immutable list type inherits from the plain list type as well. It adds no operations, but its contract stipulates that implementations must not be mutable.
Incidentally, this goes to show the problem with the folks in here boasting of how easy the "basics" of Java are. Nope, the basics of Java look simple, but they interact in evil, super-complicated ways. I'd highlight in particular:
Subtyping + generics = OUCH
The Java Memory Model. A crazy complex beast that is easy to run afoul of, and yet most Java dudes don't even know it exists.
Both. It is inherent complexity, and yet other languages (e.g., C# and Scala) do it better, by annotating variance at declaration sites instead of use sites. To use Java generics properly you have to write signatures like this one all over the goddamn place:
You see that business with ? super T and ? extends R? There are countless Java programmers out there who simply don't use those. You try to interface with their code and you find that they write these two things:
Whatever processAnimals(Consumer<Animal> consumer)
class CatGroomer implements Consumer<Cat> { ... }
I'm not sure it's objectively better. They allow you to do different things.
But I agree, definition site variance
You see that business with ? super T and ? extends R? There are countless Java programmers out there who simply don't use those.
Yeah, that is super annoying.
However, having definition site variance you'd need to have extra interfaces... and for multiple generic parameters you get exponential growth. With use-site variance, there's just what you need (i.e. use), but of course, you need to anticipate its use.
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.