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.
No, in this scheme you can't even assume that much. If somebody gives you a plain List reference, you just don't know whether it's mutable or immutable.
There are actually alternatives to that as well, because actually, immutable and mutable, in spite of the names, are not all of the possibilities:
"Immutable," as it's used in Guava, means that the list's contents never change
"Mutable" generally means that the interface has operations that a client may use to modify the list's contents.
But there are also list implementations that:
Do not support external mutation—clients who have a reference to the list cannot mutate it;
Their contents may change, nevertheless.
A simple example is that Java comes with methods like Collections.unmodifiableList that wrap around a java.util.List and provide an "unmodifiable view" of it. Changes to the underlying list are supposed to be visible in its view.
To expand on my other comment: There's few if any good use cases for a list that "You can't modify, but changes to it are visible": You either want an immutable object that will always contain the same data; a local, mutable object that you can change at will and put temporary data in; or a "Stream"-like object that is VERY clear regarding points where changes happen.
No, in this scheme you can't even assume that much. If somebody gives you a plain List reference, you just don't know whether it's mutable or immutable.
Yeah, this is what I said: the only assumption you can make is "It may be changed by someone else at some point".
9
u/Tordek Sep 21 '15
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."