On 5/28/18 9:51 AM, James Blachly wrote:
Why are the class objects special in this case, and why does
`immutable(C)[]` not help? I believed that this defined a dynamic
array `c` which was itself mutable, the elements of which were immutable.
To build on what others have said, the key thing you are missing is that
every instance of C is a *reference*. So you essentially have an array
of pointers.
If you dup the array, you are not duplicating the class instances, each
element of the array still points at the same instances! So naturally,
you can't have both a mutable and immutable reference to the same instance.
This is easily demonstrated:
auto c = [new C(), new C()];
c[0].x = 5;
auto c2 = c.dup;
c2[0].x = 6;
writeln(c[0].x); // 6
Contrast that with the struct and int arrays, where the *entire
contents* are copied.
Hope that helps.
-Steve