Comment #0 by verylonglogin.reg — 2013-04-30T05:34:45Z
---
class C
{
this() inout { }
this(inout int[]) inout { }
}
void main()
{
auto c0 = new immutable C(cast(immutable int[]) null); // OK
auto c1 = new immutable C(); // Error (line 10)
auto c2 = new immutable C(null); // Error (line 11)
}
---
dmd output:
---
main.d(10): Error: inout constructor main.C.this creates inout object, not immutable
main.d(11): Error: inout constructor main.C.this creates mutable object, not immutable
---
Partial workaround:
Mark constructor as `pure` or `immutable` if possible.
Comment #1 by k.hara.pg — 2013-05-04T01:09:08Z
These are the designed behavior.
1. If the inout constructor has inout parameters, the created object is restricted to the argument qualifiers - it is same as normal inout functions. So:
auto c0 = new immutable C(cast(immutable int[]) null); // OK
constructor call creates inout object and it is implicitly translated to immutable by the immutable arguments.
1.5 'null' literal is normally treated as a mutable data, so
auto c2 = new immutable C(null); // Error (line 11)
constructor call creates mutable C object, but it is not convertible to immutable.
2. To convert the created object to any qualifier, the constructor should have 'pure' attribute at least. So:
auto c1 = new immutable C(); // Error (line 10)
fails to compile.
Comment #2 by pro.mathias.lang — 2022-01-04T10:05:52Z
It is infuriating that the following doesn't work:
```
struct Container
{
ubyte[] value;
this (inout(ubyte)[] arg) inout @safe pure nothrow @nogc
{
this.value = arg;
}
}
immutable ubyte[] Def = [42];
immutable Container Default = Container(Def);
```
Comment #3 by pro.mathias.lang — 2022-01-04T10:06:30Z
*** Issue 21170 has been marked as a duplicate of this issue. ***
Comment #4 by robert.schadek — 2024-12-13T18:06:38Z