22

What is the difference between the const and immutable type qualifiers in D?

Charles
  • 50,943
  • 13
  • 104
  • 142
Johan Råde
  • 20,480
  • 21
  • 73
  • 110
  • 4
    You can read about immutablity on this link http://ddili.org/ders/d.en/const_and_immutable.html – yaz Jun 14 '13 at 21:43

4 Answers4

19

Something that is const cannot be mutated via that reference but could be mutated by a mutable reference to the same data. Something that is immutable can't be mutated by any reference to that data. So, if you have

const C c = foo();

then you know that you cannot mutate the object referred to by c through c, but other references to the object referred to by c may exist in your code, and if they're mutable, they could mutate it and therefore change what c sees. But if you have

immutable C c = foo();

then you know that it's not possible for the object referred to by c to change. Once an immutable object has been constructed, it's illegal for it to be mutated, and unless you subvert the type system via casting, it's not even possible to have a mutable reference to an immutable object. And since immutable objects can be put into read-only memory if the compiler chooses to, you could actually get segfaults and the like if you ever tried to cast away immutable and mutate the object. The same goes for const, since a const reference could actually refer to an immutable object. Casting away either const or immutable and then mutating the then mutable object is undefined behavior and should basically never be done.

And since an immutable object can never be mutated even by another reference, reading an immutable object from multiple threads is completely thread-safe. So, immutable objects are implicitly shared across threads, whereas everything else which isn't explicitly marked with shared is considered thread-local. immutable also provides better optimization opportunities to the compiler than const does, because it's guaranteed to never change, whereas a const object can change through another reference to the same data.

For value types, there isn't really much difference between const and immutable (since you can't have mutable references to non-mutable value types), but for reference types, there is a significant difference.

Jonathan M Davis
  • 37,181
  • 17
  • 72
  • 102
  • 1
    Thanks for a very instructive answer. Which one do D developers normally use for value types? – Johan Råde Jun 15 '13 at 17:12
  • 1
    @user763305 At the moment, I can't think of any technical reason to prefer one over the other, and I don't know what the average D developer chooses to use. Personally, I choose `immutable`, because then it's clear that it can't be mutated without having to know that it's a value type, but that's just personal preference. – Jonathan M Davis Jun 15 '13 at 20:03
  • But surely casting away `immutable` from a character of a `string` is perfectly ok? – Dmitri Nesteruk Feb 12 '14 at 06:33
  • @DmitriNesteruk If you're accessing a single `char` in a `string`, then there's no need to cast away immutable, because it's a value type, and assigning it to anything will copy it. What's dangerous is casting away `const` or `immutable` for a reference type (like a class object or an array or string). And I don't believe that you can mutate any elements in a string without casting the whole string to `char[]`, because casting a single element in an attempt assign to it would result in an rvalue, not an lvalue, so the assignment wouldn't work regardless of mutability. – Jonathan M Davis Feb 12 '14 at 11:02
14

When you declare something as const, you promise that you won't modify it. When something is declared as immutable, you get promised that it won't get modified somewhere else(and ofcourse, you can't modify it either)

Idan Arye
  • 12,402
  • 5
  • 49
  • 68
2

They are different in that immutable data, could actually placed in read-only sections of memory, and hence, any attempts to modify the data it will fail.

Something declared const (and not immutable) on the other hand exists in the r/w section and the value can still be changed via a different non-const reference to it.

So, "const-ness" can be bypassed in such a case, while immutability cannot.

(Reference)

Anirudh Ramanathan
  • 46,179
  • 22
  • 132
  • 191
  • This isn't completely correct as `const` data can actually be `immutable` originally. So `const` data may also be in read-only sections as `immutable` data casts implicitly to `const`. – yaz Jun 14 '13 at 21:41
  • @yaz Agreed. I wasn't talking about immutable being cast to const in this case. – Anirudh Ramanathan Jun 15 '13 at 05:07
1

A variable declared of type const can accept a mutable value or immutable value. This definition is relevant for referenced types like arrays and objects or pointers. It would typically be used for function arguments. So in D const is a kind of wildcard attribute for mutable and immutable values.

It doesn't have much sense for values that are copied with an assignment like a char, int or float.

The concept of const and immutable is very different from the one found in C and C++. I was very confused by this.

chmike
  • 20,922
  • 21
  • 83
  • 106