Immutability in C# Part One: Kinds of Immutability

Immutability in C# Part One: Kinds of Immutability

Rate This
  • Comments 54

I said in an earlier post that I believe that immutable objects are the way of the future in C#. I stand by that statement while at the same time noting that it is at this point sufficiently vague as to be practically meaningless! “Immutable” means different things to different people; different kinds of immutability have different pros and cons. I’d like to spend some time over the next few weeks talking about possible directions that C# could go to improve the developer experience when writing programs that use immutable objects, as well as giving some practical examples of the sort of immutable object programming you can do today.

(Again, I want to emphasize that in these sorts of “future feature” posts we are all playfully hypothesizing and brainstorming about ideas for entirely hypothetical future versions of C#. We have not yet shipped C# 3.0 and have not announced that there will ever be any future version of the language. Nothing here should be construed as any kind of promise or announcement; we’re just geeks talking about programming languages, ‘cause that’s what we do.)

So, disclaimers out of the way, what kinds of immutability are there? Lots. Here’s just a few. Note that these categories are not necessarily mutually exclusive!

Realio-trulio immutability:

There’s nothing you can do to the number one that changes it. You cannot paint it purple (‡), make it even or get it angry. It’s the number one, it is eternal, implacable and unchanging. Attempting to do something to it – say, adding three to it – doesn’t change the number one at all. Rather, it produces an entirely different and also immutable number. If you cast it to a double, you don’t change the integer one; rather, you get a brand new double.

Strings, numbers and the null value are all truly immutable.

C# allows you to declare truly immutable named fields with the const keyword. The compiler ensures that the only things that are allowed to go into const fields are truly immutable things – numbers, strings, null. (See the section of the standard on “constant expressions” for details.)

Write-once immutability:

Fields marked as const have to be compile-time constants, which is a bit of a pain if what you want to do is have a field which never changes but nevertheless cannot be computed until runtime. For example, in a later post I’m going to define an immutable stack class which has this code:

    public sealed class Stack<T> : IStack<T>
    {
        private sealed class EmptyStack : IStack<T>
        { /* ... */ }
        private static readonly EmptyStack empty = new EmptyStack();
        public static IStack<T> Empty { get { return empty; } }

I will want to create a singleton empty stack. Clearly it is not a compile-time constant, so I cannot make the field const. But I want to say “once this thing is initialized it is never going to change again.” That’s what the readonly modifier ensures. Basically it’s a “write only once” field. Not exactly immutable, since obviously it changes exactly once, from null to having a value. But pretty darn immutable.

Popsicle immutability:

...is what I whimsically call a slight weakening of write-once immutability. One could imagine an object or a field which remained mutable for a little while during its initialization, and then got “frozen” forever. This kind of immutability is particularly useful for immutable objects which circularly reference each other, or immutable objects which have been serialized to disk and upon deserialization need to be “fluid” until the entire deserialization process is done, at which point all the objects may be frozen.

There is at present no really universal convention for how to declare a freezable object, and there certainly is no support in the compiler for this kind of immutability.

Shallow vs deep immutability:

Consider a write-once field containing an array:

public class C {
    private static readonly int[] ints = new int[] { 1, 2, 3 };
    public static int[] Ints { get { return ints; } }

The value of the field cannot be changed; C.ints = null; would be illegal even from inside the class. This is a sort of “referential” immutability. But there is nothing immutable at all about the array itself! C.Ints[1] = 100; is still perfectly legal from outside the class.

The ints field is “shallowly” immutable. You can rely upon it being immutable to a certain extent, but once you reach a point where there is a reference to a mutable object, all bets are off.

Obviously the opposite of shallow immutability is “deep” immutability; in a deeply immutable object it is immutable all the way down.

If we had immutability in the type system, something like the far stronger kind of “const” in C/C++, then a hypothetical future compiler could verify that an object marked as deeply immutable had only deeply immutable fields.

Objects which are truly madly deeply immutable have a lot of great properties. They are 100% threadsafe, for example, since obviously there will be no conflicts between readers and (non-existant) writers. They are easier to reason about than objects which can change. But their strict requirements may be more than we need, or more than is practical to achieve.

Immutable facades:

Since the contents of an array (though, interestingly enough, not its size) may be changed arbitrarily, it’s a bad idea to expose data that you want to be logically read-only in a public array field. To make this a bit easier, the base class library lets you say

public class C {
    private static readonly intarray = new int[] { 1, 2, 3 };
    public static readonly ReadOnlyCollection<int> ints = new ReadOnlyCollection<int>(intarray);
    public static ReadOnlyCollection<int> Ints { get { return ints; } }

The read-only collection has the interface of a regular collection; it just throws an exception every time a method which would modify the collection is called. However, clearly the underlying collection is still mutable. Code inside C could mutate the array members.

Another down side of this kind of immutability is that the compiler is unable to detect attempts to modify the collection. Attempts to, say, add new members to the collection will fail at runtime, not at compile time.

This sort of immutability is a special case of...

Observational immutability:

Suppose you’ve got an object which has the property that every time you call a method on it, look at a field, etc, you get the same result. From the point of view of the caller such an object would be immutable. However you could imagine that behind the scenes the object was doing lazy initialization, memoizing results of function calls in a hash table, etc. The “guts” of the object might be entirely mutable.

What does it matter? Truly deeply immutable objects never change their internal state at all, and are therefore inherently threadsafe. An object which is mutable behind the scenes might still need to have complicated threading code in order to protect its internal mutable state from corruption should the object be called on two threads “at the same time”.

Summing up:

Holy goodness, this is complicated! And we have just barely touched upon the deeply complex relationship between immutability of objects and “purity” of methods, which opens up huge cans of worms.

So, smart people, what do you think? Are there forms of immutability which I did not touch upon here that you like to take advantage of in your programs? Are there any particular forms of immutability which you would like to see made easier to use in C#?

Next time: let’s get a little more practical. I already implemented an immutable stack in my A* series, but that was pretty special-purpose. We’ll take a look at how one might implement a general-purpose immutable stack today in C# 3.0. We'll then expand that to immutable queues, trees, etc. (And I might even discuss how one could take advantage of typesafe covariance when designing interfaces for immutable data structures, oh frabjous day!)

(‡) A dear old friend of mine from school who happens to be a grapheme-colour synaesthete tells me that of course you cannot paint the number one purple because it is already blue. Silly me!

  • I certainly take your larger point to heart -- which, I might paraphrase as "good design is the art of compromising between conflicting goals, so please find out what my needs are and prioritize them".  This is certainly what we try to do every day: figure out our customers' needs, and choose designs which prioritize them.

    However, I feel compelled to point out that your smaller point is a non sequitur.  The problem with string concatenation being expensive has practically nothing to do with the fact that strings are immutable. Rather, it is a consequence of two things. First, that strings are fixed length, and second, that they are implemented as contiguous memory buffers. That gives us three ways to attack the problem.

    The first would be to make strings into non-fixed length mutable structures that use a double-when-full strategy.  That makes string concatenation O(1) on average, but it also means that

    s1 = "hello"

    s2 = s1 + "goodbye"

    would either (a) change the value stored in s1, or (2) copy the value stored in s1, and hey, guess what, we're back to an inefficient concatenation algorithm again. I hope that you would reject both of these options.

    The second would be to make the underlying implementation of trees into, say, the immutable deque I developed in part eleven of this series. This gives cheap concatenation at the expense of making it immensely expensive to interrogate the interior of the string. I hope you would reject this too.  Favouring one common operation at the massive expense of another is a bad balancing of competing goals if that massive expense cannot be avoided by other means.

    I once spent an entire summer rewriting the VBScript string library to use immutable trees instead of immutable BSTRs. It was a disaster. The performance overhead of maintaining the trees was immense compared to the tiny savings of making pages that did a million concatenations faster.

    The third approach would be to keep the good performance and value-like semantics by using an underlying implementation that uses immutable fixed-size character arrays, and addressing the quadratic nature of the concatenation operator by providing a highly efficient and well-tuned alternative. Some sort of "string builder" class, you might say.

    Design is, as I said, the art of compromising. Implementing strings as immutable character arrays is the best choice that balances out all these competing design goals; had we picked something else, you'd be complaining MORE about it. That there is no perfect solution to this problem is a fact about the nature of computation, not about our inability to make good design choices.

    The point of this whole series is that programming in C# using immutable data structures is:

    * simpler to use than mutable data structures for many use cases

    * far easier to reason about than mutable structures

    * make development faster by reducing the amount of time tracking down mutation bugs or reasoning about mutation semantics

    * trade off understandability, robustness, and applicability in rich scenarios against performance of memory allocation, which is typically cheap and fast anyway.

    Is it a panacea? No. Should you be forced to use it? Of course not. Is it an incredibly valuable tool that you should have in your toolbox, sharp and ready for use? Yes. Should you be able to understand it when other people use it?  Oh my yes, you will see a lot more coding in this style in C# in the future, I predict.

  • Okay, I know I said that part 4 would be the last part in this series ... but since then I&#39;ve not

  • This is really a good article. Thnkx for telling about immutable objects

  • C#中的不变类型 ThereisapowerfulandsimpleconceptinprogrammingthatIthinkisreallyunderused:...

  • I've been reading Eric Lippert's series on immutable collections (start here with part one ) over on his blog, Fabulous Adventures in Coding . I don't understand everything he writes, but it's still a fascinating read. This morning on my commute I was

  • Another very simple pattern builds on the foundation of the Safe-Unsafe Cache pattern .&#160; What is

  • My port of the Protocol Buffers project has proved pretty interesting. I thought I&#39;d share some of

  • Hi!

    I've been looking around for a simple example of the "right way" to implement immutable object xml serialization/deserialization.

    Since IXmlSerializable.ReadXml(System.Xml.XmlReader reader) requires the object inner state to be changed, I have to make private fields writable, which I would like to avoid. I've tried googling it but was unable to find the answer I was looking for.

    Thanks a lot,

    Veki

  • How to implement Array Immutability in C#?

    wisentechnologies.com/.../.net-training.aspx

Page 4 of 4 (54 items) 1234