3

I regularly write classes which can only have immutable instances, much like string.

I am wondering why Java or C# or VB.NET don't have immutability built-in into the language? That way, I can communicate immutability better to my fellow programmers and also make assumptions about state in the class.

For instance: immutable class Foo { public string Name { get; set; } //compile error: mutable property on immutable class }

Ruudjah
  • 558
  • 1
  • 5
  • 11
  • 1
    ...see also: [If immutable objects are good, why do people keep creating mutable objects?](http://programmers.stackexchange.com/q/151733/31260) and [At what point do immutable classes become a burden?](http://programmers.stackexchange.com/q/68058/31260) – gnat Jan 24 '15 at 19:20
  • Strings in Java are immutable – sakisk Jan 24 '15 at 19:32
  • @faif Implicitly so (as far as the language), since it is carefully written so that (1) none of the code in the class itself modifies an instance's value and (2) all state is properly hidden and not aliased, so that (non-malicious) outside of the class can't modify it either. What Ruudjah is getting at is some language feature that automated or checks or otherwise supports these properties. –  Jan 24 '15 at 19:35
  • 1
    C++ has `const`, not exactly what you want, but near. – Basile Starynkevitch Jan 24 '15 at 20:29
  • 1
    What? Java has the `final` keyword. If an object's state is all final, and the classes (e.g. string, primitives, wrappers) that comprise its members all have final state, then your object is very likely immutable. That is built-in to the language. –  Jan 24 '15 at 20:37

1 Answers1

6

C# does have immutability built in. So does Java, and VB.NET.

Mutable:

public class Counter
{
   private int count;

   public Counter(int initialCount)
   {
       count = initialCount;
   }

   public void Increment()
   {
       count++;
   }
}

Immutable:

public class Counter
{
   private readonly int count;  // `final` for Java

   public Counter(int finalCount)
   {
       // set at construction time, but readonly, so can't modify.
       count = finalCount;
   }

   public void Increment()
   {
       // Invalid operation
       count++;
   }
}

Note that I'm using "immutability" hear to mean "set once during object construction." Constants are not very useful in this context, because they can only be set at compile-time.

If you dig deep enough, every programming language mutates state under the hood, because that's how the processor instructions are designed.

Robert Harvey
  • 198,589
  • 55
  • 464
  • 673
  • 3
    I think what the OP was actually thinking about was a way to declare a *whole class* as immutable, not just individual attributes of that class. – Philipp Jan 24 '15 at 21:09
  • "If you dig deep enough, every programming language mutates state under the hood, because that's how the processor instructions are designed.": If a programming language does not allow mutation, the fact that its implementation does use mutation is irrelevant for the programmer. – Giorgio Jan 24 '15 at 21:10
  • I guess what the OP probably meant was "why do mainstream OO languages don't have immutability (or immutable variables) by default." (like Haskell). – Doc Brown Jan 24 '15 at 21:12
  • 1
    @Philipp: What do you mean by _whole class_? Are you referring to the possibility to change a class at runtime, e.g. adding new methods? – Giorgio Jan 24 '15 at 21:12
  • 1
    @Giorgio No, I was thinking about a keyword in the class definition which results in the class no longer being allowed to have non-final / non-readonly attributes (not that I believe that this would be a good idea). – Philipp Jan 24 '15 at 21:25
  • @Philipp the transitive immutability is non-trivial. If you have a read-only ArrayList, the array list itself can still be changed, and the things in the array list can still be changed making it non-immutable. Trying to do that is very non-trivial. –  Jan 25 '15 at 20:10
  • @MichaelT That's one of the reasons I said that I don't believe it to be a good idea. But you could tackle this problem by adding the requirement that in an immutable class, any attributes must also be immutable types. So you would only be allowed to have an `ImmutableArrayList` in such a class. – Philipp Jan 25 '15 at 20:12
  • @Philipp You might also want to read [Patent on "safe" transitive immutability for object types prior art?](http://lambda-the-ultimate.org/node/5028) from Lambda the Ultimate. Also give [Prior art for US patent application 20140196008 'IMMUTABLE OBJECT TYPES'](http://patents.stackexchange.com/q/10128/8623) from Patents.SE a read. It points out some examples of what exists and clarifications about what is claimed in that patent. –  Jan 25 '15 at 20:15