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 }

5
  • 1
  • Strings in Java are immutable
    – sakisk
    Commented Jan 24, 2015 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.
    – user7043
    Commented Jan 24, 2015 at 19:35
  • 1
    C++ has const, not exactly what you want, but near. Commented Jan 24, 2015 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.
    – user22815
    Commented Jan 24, 2015 at 20:37

1 Answer 1

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.

8
  • 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
    Commented Jan 24, 2015 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
    Commented Jan 24, 2015 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
    Commented Jan 24, 2015 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
    Commented Jan 24, 2015 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
    Commented Jan 24, 2015 at 21:25

Not the answer you're looking for? Browse other questions tagged or ask your own question.