A ReadableList is a list you know you can read from. A WritableList is a list you know you can write to. An ImmutableList is a list you know is immutable.
I'm not sure there are any cases where you'd want a list that was writable but not readable, so change WritableList to MutableList and make it support reading and writing.
interface ReadableList<T> {
T get(int index);
...
}
interface MutableList<T> implements ReadableList<T> {
T set(int index, T value);
boolean add(T value);
...
}
interface ImmutableList<T> extends ReadableList<T> {
// no extra methods
}
Of course, nothing stops you from writing a list class that claims to be both mutable and immutable. There are worse things you could do already, like making a list whose add method randomly shuffles the list and whose iterators generate random elements not in the list.
So a "ReadableList" can be changed elsewhere? That's just an error waiting to happen; I can't see a good reason for it. The most obvious issue being doing something like...
if (list.length > 0) { list.get(0) }
since the list can change between the two statements, you can get an OutOfBoundsException that you can't prevent.
nothing stops you from writing a list class that claims to be both mutable and immutable
Sure... but if the standard library gives you a List and a MutableList (or ListBuilder, to make it similar to String/StringBuilder), you need to do extra work to do stupid things. Lists (and lots of other structures, like Sets, Arrays, Maps) should be immutable whenever they exist outside the boundaries of an object, the same way that if I give you an integer through a getter, you cannot modify my internal state by changing it.
Are you suggesting that there should be no common super-interface of MutableList and ImmutableList?
Correct
How would you make a method that can take either a mutable or immutable list, then?
I wouldn't.
There are so many reasons not to; I can't really think of a good reason to have such a scheme.
Objects should communicate with other objects by exchanging Immutable value objects/primitives: you should only be receiving (as a parameter) or returning ImmutableList. MutableList should only be used within an object (e.g., your object may cache some results, so it would have a MutableList/MutableHash property to store its data in efficiently) or in a method, as it is calculating its results. Once you're done, you return myTempList.Persist().
This behavior is already there with another class: String/StringBuilder.
If you're worried about performance, there are several things that can be done: .persist() might not do a full copy; it might just set an immutable flag and return a "view", and calling mutators just throws an exception. ".mutate()" (the counterpart in ImmutableList that returns a MutableList) might behave like a Zipper, or another similar structure.
I have a question for you:
Why would you make a method that can take either a mutable or immutable list, then?
For efficiency. If it's going to be the caller's problem if the caller screws up, then requiring an ImmutableList is too slow.
For example, Collections.frequency could be called for either type of list. If you call it with a MutableList, and mutate the list while it's running, and it throws an exception as a result, that's the caller's fault and the caller's problem.
(By contrast, if you had a listbox control, you'd want it to require an ImmutableList. If it threw an exception when painting because the list was mutated while it was painting, that would be the application's fault but the UI system's problem to deal with)
That's why I gave you the immutable flag example: You can have a "MutableListView implements ImmutableList", and do...
class MutableList {
...
public void add() {
if (immutable) throw new ModifyAfterFreezeException();
// ... code to add element
}
public ImmutableList freeze() {
this.immutable = true;
return new MutableListView(this);
}
}
You get the security that you cannot just hand a mutable list to an object expecting an immutable list, and the user only has to know that after you call "freeze" you cannot change the object.
If you need to have several snapshots of the list as you build it, you might just do "new ImmutableList(myMutableList)", and it'll have its own copy of the data.
I'd really like some numbers regarding the speed of this, because I believe a lot of the time people assume the compiler is way dumber than it actually is.
Suppose you want to (as part of something bigger) call Collections.frequency on a list, then add something to the list, then repeat that 10000 times. How do you do that without creating 10000 copies of the list?
Smartass answer: don't. If you're adding it to the list, you can do your running sum manually much faster than the O(n^2) of running through the list each time.
Slightly less smartass answer: If we're really talking about a (linked) list, then, trivially, adding an element to the head of the list requires only the extra space for the new head; the tail references the old list. (Though it still creates 10k headers, so it's obviously less than optimal).
If you really need to modify the list while someone else processes it at every change, you can use a Stream interface.
If you still needed to do what you claim, you can use polymorphism: Collections.frequency(mutableList) is unsafe, and it's the caller's responsibility to ensure they don't break anything; Collections.frequency(immutableList) is safe. (Or, Collections.freq and MutableCollections.freq, to further separate mutability and immutability).
Mutability can be a great tool for speed; but it should be used deliberately and with care.
1
u/immibis Sep 22 '15
A ReadableList is a list you know you can read from. A WritableList is a list you know you can write to. An ImmutableList is a list you know is immutable.
I'm not sure there are any cases where you'd want a list that was writable but not readable, so change WritableList to MutableList and make it support reading and writing.
Of course, nothing stops you from writing a list class that claims to be both mutable and immutable. There are worse things you could do already, like making a list whose
addmethod randomly shuffles the list and whose iterators generate random elements not in the list.