Issues with immutable/inout

burt invalid_email_address at cab.abc
Fri May 15 12:33:05 UTC 2020


On Friday, 15 May 2020 at 11:08:36 UTC, Timon Gehr wrote:
> On 15.05.20 12:13, burt wrote:
>> Hello,
>> 
>> Since D's const, immutable and inout are transitive, it is 
>> more difficult to write const-correct code.
>
> There's no such thing. This is a C++-ism.

Oh, I did not know that. I assumed it referred to marking the 
right things const to signal it won't or shouldn't mutate.

>> ...
>> 
>> Problem (1): class constructors cannot be called with 
>> different mutabilities. When marking the constructor as 
>> mutable, you cannot create immutable objects; when marking as 
>> inout or immutable, it doesn't work either.
>> ...
>
> `inout` is supposed to work. I think this is a regression. Not 
> sure when it happened but it was between 2.060 and 2.070.1.

According to run.dlang.io, it stopped working in 2.063. I'm 
guessing it was caused by 
https://dlang.org/changelog/2.063.html#ctorqualifier. I can't 
really find out what PR caused it though.

> ...
> Mark the constructors `pure` and it will work.

Thanks, but this appears to only work with non-reference types:
```
//Compared to this:
//alias T = int;
alias T = char[];

class B
{
     T x;
     this(inout T x) inout pure { this.x = x; }
}

void main() {}

unittest
{
     auto b = new immutable B(T.init);
}
```
(Compile with -unittest)



More information about the Digitalmars-d mailing list