C++ mutable in D

Michael Galuza riddlermichael at gmail.com
Sun Aug 1 18:21:32 UTC 2021


On Friday, 30 July 2021 at 19:17:55 UTC, Michael Galuza wrote:
> Is there any analogue of C++ `mutable` in D? As far as I 
> understand, this is impossible due to transitive constness, but 
> maybe some variation of `Rebindable` will be useful?
>
> P.S. Of course I mean 'fair' mutable, not qualifier-away 
> `cast()`.

Let's slightly reformulate my question. Can we write in D smth 
like this:
```
struct Mutable(T) { /* magic */ }

struct Shape {
     private Mutable!double area;

     double getArea() const {
         if (area == 0) { area = computeArea(); }
         return area;
     }
}
```

In other words, can we implement method `void S.opAssign(T value) 
const` of some struct `S` which change internal state of `S` and 
this method doesn't have UB and doesn't break D's type system.


More information about the Digitalmars-d mailing list