Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Immutable object
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== D === In [[D (programming language)|D]], there exist two [[type qualifier]]s, <code>const</code> and <code>immutable</code>, for variables that cannot be changed.<ref name="d_spec_const">[https://dlang.org/spec/const3.html D Language Specification Β§ 18]</ref> Unlike C++'s <code>const</code>, Java's <code>final</code>, and C#'s <code>readonly</code>, they are transitive and recursively apply to anything reachable through references of such a variable. The difference between <code>const</code> and <code>immutable</code> is what they apply to: <code>const</code> is a property of the variable: there might legally exist mutable references to referred value, i.e. the value can actually change. In contrast, <code>immutable</code> is a property of the referred value: the value and anything transitively reachable from it cannot change (without breaking the type system, leading to [[undefined behavior]]). Any reference of that value must be marked <code>const</code> or <code>immutable</code>. Basically for any unqualified type <code>T</code>, <code>const(T)</code> is the disjoint union of <code>T</code> (mutable) and <code>immutable(T)</code>. <syntaxhighlight lang="d"> class C { /*mutable*/ Object mField; const Object cField; immutable Object iField; } </syntaxhighlight> For a mutable <code>C</code> object, its <code>mField</code> can be written to. For a <code>const(C)</code> object, <code>mField</code> cannot be modified, it inherits <code>const</code>; <code>iField</code> is still immutable as it is the stronger guarantee. For an <code>immutable(C)</code>, all fields are immutable. In a function like this: <syntaxhighlight lang="d"> void func(C m, const C c, immutable C i) { /* inside the braces */ } </syntaxhighlight> Inside the braces, <code>c</code> might refer to the same object as <code>m</code>, so mutations to <code>m</code> could indirectly change <code>c</code> as well. Also, <code>c</code> might refer to the same object as <code>i</code>, but since the value then is immutable, there are no changes. However, <code>m</code> and <code>i</code> cannot legally refer to the same object. In the language of guarantees, mutable has no guarantees (the function might change the object), <code>const</code> is an outward-only guarantee that the function will not change anything, and <code>immutable</code> is a bidirectional guarantee (the function will not change the value and the caller must not change it). Values that are <code>const</code> or <code>immutable</code> must be initialized by direct assignment at the point of [[declaration (computer programming)|declaration]] or by a [[constructor (object-oriented programming)|constructor]]. Because <code>const</code> parameters forget if the value was mutable or not, a similar construct, <code>inout</code>, acts, in a sense, as a variable for mutability information. A function of type <code>const(S) function(const(T))</code> returns <code>const(S)</code> typed values for mutable, const and immutable arguments. In contrast, a function of type <code>inout(S) function(inout(T))</code> returns <code>S</code> for mutable <code>T</code> arguments, <code>const(S)</code> for <code>const(T)</code> values, and <code>immutable(S)</code> for <code>immutable(T)</code> values. Casting immutable values to mutable inflicts undefined behavior upon change, even if the original value comes from a mutable origin. Casting mutable values to immutable can be legal when there remain no mutable references afterward. "An expression may be converted from mutable (...) to immutable if the expression is unique and all expressions it transitively refers to are either unique or immutable."<ref name="d_spec_const"/> If the compiler cannot prove uniqueness, the casting can be done explicitly and it is up to the programmer to ensure that no mutable references exist. The type <code>string</code> is an alias for <code>immutable(char)[]</code>, i.e. a typed slice of memory of immutable characters.<ref>[https://dlang.org/spec/arrays.html#strings D Language Specification Β§ 12.16] (The terms ''array'' and ''slice'' are used interchangeably.)</ref> Making substrings is cheap, as it just copies and modifies a pointer and a length filed, and safe, as the underlying data cannot be changed. Objects of type <code>const(char)[]</code> can refer to strings, but also to mutable buffers. Making a shallow copy of a const or immutable value removes the outer layer of immutability: Copying an immutable string (<code>immutable(char[])</code>) returns a string (<code>immutable(char)[]</code>). The immutable pointer and length are being copied and the copies are mutable. The referred data has not been copied and keeps its qualifier, in the example <code>immutable</code>. It can be stripped by making a depper copy, e.g. using the <code>dup</code> function.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)