Issues with immutable/inout

Timon Gehr timon.gehr at gmx.ch
Fri May 15 11:08:36 UTC 2020


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.

> ...
> 
> 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.

> Problem (2): cannot (implicitly/safely) convert between immutable and 
> mutable objects, even if the class is final and contains only immutable 
> fields. It is bad that immutable applies to the class reference, and not 
> to the underlying object.
> ...

I agree. One way to make some progress on this would be to give special 
semantics to `immutable`/`shared`/... classes. (I.e., enable conversion 
between references of different mutabilities.)

> I feel like D would be a lot more usable for me if these two issues were 
> fixed. However, fixing problem 2 probably requires a change in the spec 
> and in the language. Does such a change require a DIP or is a bug report 
> enough? Or is there a different (@safe) solution that I overlooked?
> 

Mark the constructors `pure` and it will work.



More information about the Digitalmars-d mailing list