What is the difference between the const
and immutable
type qualifiers in D?
相关问题
- Creating Immutable Classes with Multiple Construct
- Lazily Reading a File in D
- Using MongoDB to store immutable data?
- Using a foreach loop — variable cannot be read
- Returning a mutable reference that is behind an im
相关文章
- Understanding immutable composite types with field
- MPI and D: Linker Options
- How do I modify a record in erlang?
- Why BigInteger in java is designed to be immutable
- Scala: var List vs val MutableList
- Can an immutable type change its internal state?
- CDB command for setting a breakpoint based on a li
- Why isn't “is” comparison used in place of “==
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.
Something that is
const
cannot be mutated via that reference but could be mutated by a mutable reference to the same data. Something that isimmutable
can't be mutated by any reference to that data. So, if you havethen you know that you cannot mutate the object referred to by
c
throughc
, but other references to the object referred to byc
may exist in your code, and if they're mutable, they could mutate it and therefore change whatc
sees. But if you havethen you know that it's not possible for the object referred to by
c
to change. Once animmutable
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 animmutable
object. And sinceimmutable
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 awayimmutable
and mutate the object. The same goes forconst
, since aconst
reference could actually refer to animmutable
object. Casting away eitherconst
orimmutable
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 animmutable
object from multiple threads is completely thread-safe. So,immutable
objects are implicitly shared across threads, whereas everything else which isn't explicitly marked withshared
is considered thread-local.immutable
also provides better optimization opportunities to the compiler thanconst
does, because it's guaranteed to never change, whereas aconst
object can change through another reference to the same data.For value types, there isn't really much difference between
const
andimmutable
(since you can't have mutable references to non-mutable value types), but for reference types, there is a significant difference.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)
When you declare something as
const
, you promise that you won't modify it. When something is declared asimmutable
, you get promised that it won't get modified somewhere else(and ofcourse, you can't modify it either)