dynamic arrays of immutable question: Are the elements actually mutable

monarch_dodra monarchdodra at gmail.com
Wed Jan 9 10:17:51 PST 2013


On Wednesday, 9 January 2013 at 13:38:14 UTC, monarch_dodra wrote:
>
> The original line I am concerned about is "HERE": Is this a 
> legal mutation?

I think I got my answer actually: No. No it isn't. Changing the 
array's length initialize the not yet typed memory to T.init, 
ergo, it types the memory, so changing it again would be illegal 
(AFAIK).

This has not failed on any platform yet though.

//----
import std.stdio;


struct S
{
     int i = 9;	
}

void main()
{
     //The initial array
     immutable(S)[] arr1 = [S(0)];

     size_t capacity = arr1.capacity;
     writeln(arr1.ptr[0 .. arr1.capacity]);
     arr1.length = arr1.capacity;
     writeln(arr1);
}
//----
[immutable(S)(0), immutable(S)(10991), immutable(S)(493355328)]
[immutable(S)(0), immutable(S)(9), immutable(S)(9)]
//----

The "good news" is that since the values are T.init'd, then 
opEqual becomes somewhat more valid, although arguably, it should 
really be in-place construction, *especially* if immutable is 
involved (or types without opAssign...).


More information about the Digitalmars-d-learn mailing list