6

If you have an immutable list, you expect it to always return a reference to the same object when you ask for, say

list.get(0)

My question is, would you expect to be able to mutate the object and have the mutation reflected next time you get it from the list?

jjujuma
  • 22,055
  • 12
  • 44
  • 46
  • Not really an answer, but an interesting link: [F# List](http://msdn.microsoft.com/en-us/library/dd233224.aspx). In F# immutability is a big thing. "A list in F# is an ordered, immutable series of elements of the same type", while later on that page they fill the list with mutable objects (i.e. Button and CheckBox). – Matthijs Wessels Mar 11 '13 at 19:01
  • Might want to see [immutable-or-not-immutable](http://stackoverflow.com/questions/9009627/immutable-or-not-immutable) as well. – nawfal Jul 16 '14 at 09:38

7 Answers7

8

It depends on the context. In a general purpose library, all we should assume is that the list is immutable. Changes to the elements in the list would be reflected to all callers, as a direct consequence of returning the same reference each time.

However, if this is a specialized immutable tree (or whatever), and is documented as such then you would expect the items in the list to themselves be immutable, and it becomes a moot question.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
2

The question if not about the immutability of the list, but about the immutability of the objects contained.

In fact, if you have reference types, the immutable entity in the list is the reference. This means that the reference will always be the same. Now whether the referenced object changes only depends on what kind of object it is. If the object is immutable (like, for instance, strings in both .NET and Java, or all value types in .NET), the object cannot change.

Otherwise, the object can change and all other references to the same object will see the changed state, since they hold a reference to the same instance. Therefore, as I wrote in the beginning, this is completely independent of the list (and whether it is immutable or not).

Lucero
  • 59,176
  • 9
  • 122
  • 152
  • "... immutable (like, (...) or all value types in .NET)" Aren't structs mutable value types in .NET? – Matthijs Wessels Mar 06 '13 at 23:13
  • @MatthijsWessels: In the context of the question they are immutable; you get a copy each time and even if you change the copy of the struct, it's not going to change the list item. – Lucero Mar 07 '13 at 10:43
  • Ah I see, that's true. What if the struct has a member that is a reference type? – Matthijs Wessels Mar 07 '13 at 22:12
  • @MatthijsWessels, then you might be able to mutate the instance referenced by the struct, but not the reference itself (e.g. you couldn't set it to another instance or `null`). – Lucero Mar 08 '13 at 09:52
  • Hmm, I guess then we're back at the original question whether an object is immutable if some of its members are mutable and not defensivly copied. – Matthijs Wessels Mar 11 '13 at 14:25
  • @MatthijsWessels Sure. Following the naming for "shallow" vs. "deep" cloning, I'd say that there is only support for shallow immutable objects and not deep immutable objects in .NET. – Lucero Mar 11 '13 at 15:21
1

Suppose a repair shop wanted to keep a permanent append-only record of all the cars that had ever visited, so that as each car entered the owners could find out if it had been in the shop before. Which would be better:

  1. Keep a permanent record of the VIN (Vehicle Identification Number) of each car
  2. Make a duplicate of each car (which, because VINs must be unique, must have a different VIN from the original) and permanently store the duplicate cars.

Note that a car itself is a mutable object, but the identity of a car, expressed by the VIN, is immutable. It's entirely possible that a car which was blue when it visited the shop has since been painted red. Thus, even if one had the ability to easily locate any car given its VIN, a list of cars (VINs) and when they were in the shop would not allow one to determine e.g. how many blue cars were serviced last Thursday. On the other hand, if the purpose of the list is to let one know whether an incoming vehicle had been in the shop previously, the list of VINs is exactly what one would need. If instead of having the VINs, one had a collection of duplicate cars, then not only would the cost of creating and storing all those duplicate cars be far greater than the cost of storing the VINs, but the collection would be almost useless for the stated purpose (determining whether a particular car had visited previously).

supercat
  • 77,689
  • 9
  • 166
  • 211
  • You could also make cars immutable. Then you can still only store the VINs in your record. The only thing then is that you would have to buy a new car if you want to change anything about your car (e.g. the color). – Matthijs Wessels Mar 11 '13 at 20:12
  • @MatthijsWessels: How would an immutable car transport mutable passengers? If passengers were immutable and cloneable, a car containing three passengers in it that was in Los Angeles could be used to produce a copy of the car, containing copies of passengers, that was identical to the first except for being in New York. If passengers aren't immutable and cloneable, making a new copy of a car in New York wouldn't do anything for the passengers who were still inside the Los Angeles original. – supercat Mar 11 '13 at 20:32
  • Well, you wouldn't have to copy the passengers, you could throw all the passengers on a big heap and just copy their passport numbers... ok ok, I'm stretching the real life example a bit far :). But you are right that if the repair shop wants to keep track of a lot of things about the car that change a lot (e.g. color, passengers, current longtitude/langtitude, etc), that immutable cars are not a good idea. – Matthijs Wessels Mar 11 '13 at 23:14
  • Suppose Bob and Joe both own the same car. If one says `Bob.PrimaryVehicle.Location = "Los Angeles"`, that should also set `Joe.PrimaryVehicle.Location` to "Los Angleles". Such things may be easily modeled with mutable objects, and are very hard to model without them. – supercat Mar 11 '13 at 23:23
  • @MatthijsWessels: Immutable objects are good for holding moment-in-time information about things that may change, but are far less good for representing changeable things themselves. One could use immutable car objects if one had a mutable table which mapped VINs to car objects, such that a given VIN might today represent a car which is in Phoenix and might tomorrow represent a car which is in Seattle. One could even get by if there were a single mutable reference to an immutable table holding that information. Something in the chain, however, has to be mutable. – supercat Mar 12 '13 at 15:28
1

That's usually to be expected. The list is immutable, which means you cannot add or remove items in it or replace items entirely. If you want those items to be immutable you have to take care of that yourself. The list certainly can't stop you from mutating the object's state once you got a reference to it.

Joey
  • 344,408
  • 85
  • 689
  • 683
1

Yes.

I don't expect an immutable list to clone its objects when I get them, unless it is documented as doing so.

Stephen Denne
  • 36,219
  • 10
  • 45
  • 60
1

It really depends on the context in which you ask that question. Any experienced Java or C# developer knows that it's technically almost impossible to have a general "deep immutability" and therefore would not expect this. In C++, it's a very complex topic, so most developers probably also don't expect a dependable deep immutability. The D programming language, on the other hand, does have a language-level concept of transitive immutability, so a D programmer would probably expect it wherever it makes sense (which is quite often).

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
0

Yes, knowing Java, for an "immutable" List<T> I wouldn't expect T to be immutable unless T was immutable. However, a reasonable implementation of, say, List<Date> would be to copy the Date each time. The problem is that Date is mutable and can be distinguished from other equal Dates.

Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305