Question on Immutability

jfondren julian.fondren at gmail.com
Tue Aug 31 06:15:07 UTC 2021


On Tuesday, 31 August 2021 at 05:42:22 UTC, ag0aep6g wrote:
> On 31.08.21 02:50, Mike Parker wrote:
>> Member functions marked as immutable can be called on both 
>> mutable and immutable instances.
>
> That's not true.

Demonstrated:

```d
struct S {
     int x;
     int get() immutable { return x; }
}

unittest {
     auto s1 = S(1);
     const s2 = S(2);
     immutable s3 = S(3);
     assert(s1.get == 1); // Error: is not callable using a 
mutable object
     assert(s2.get == 2); // Error: is not callable using a 
`const` object
     assert(s3.get == 3);
}
```

s/immutable/const/ and all those uses are acceptable.


More information about the Digitalmars-d-learn mailing list