Search⌘ K

Constructor Qualifiers

Explore how constructor qualifiers in D modify the initialization of mutable, const, immutable, and shared objects. Understand the role of type constructors in defining qualified types and their impact on performance and correctness during object construction.

We'll cover the following...

Normally, the same constructor is used for mutable, const, immutable, and shared objects:

D
import std.stdio;
struct S {
this(int i) {
writeln("Constructing an object"); }
}
void main() {
auto m = S(1);
const c = S(2);
immutable i = S(3);
shared s = S(4);
}

Semantically, the objects that are constructed on the right-hand sides of those expressions are all mutable, but the variables have different type qualifiers. The same constructor is used for all of them.

Depending on the qualifier of the resulting object, sometimes some members ...