dynamic arrays of immutable question: Are the elements actually mutable

monarch_dodra monarchdodra at gmail.com
Wed Jan 9 04:29:43 PST 2013


I was investigating the inner working of appender, and I'm not 
100% sure what it is doing with arrays of immutables is 
completely kosher.

However, I'm not 100% how arrays of immutables work.

In particular, since dynamic arrays can be appended to, by 
design, the elements can be mutated, right?
I mean, take this program:
//----
     immutable(int)[] arr = [1, 2];
     // data in memory [1, 2, 0, 0, 0, ...

     arr ~= 3;
     // data in memory [1, 2, 3, 0, 0, ...

     arr ~= 4;
     // data in memory [1, 2, 3, 4, 0, ...
//----
I don't actually know if the data is 0 initilized, but I figure 
it is irrelevent: The point is that the data *has* to be mutated 
for appending, so the data can't be immutable.

So my question is:
If I by-pass compiler protections via casting, is modifying the 
elements of a dynamic array* of immutables actually defined 
behavior ?

* Assuming GC allocated dynamic array, and not slice of static 
immutable array, for example.

http://dpaste.dzfl.pl/41eeeacf


More information about the Digitalmars-d-learn mailing list