Reputation: 21408
What is the difference between the const
and immutable
type qualifiers in D?
Upvotes: 25
Views: 2881
Reputation: 22154
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.
Upvotes: 1
Reputation: 38287
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.
Upvotes: 21
Reputation: 46738
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.
Upvotes: 3
Reputation: 12613
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)
Upvotes: 16