dynamic arrays of immutable question: Are the elements actually mutable

ollie ollie at home.net
Wed Jan 9 09:22:25 PST 2013


On Wed, 09 Jan 2013 14:38:13 +0100, monarch_dodra wrote:

> On Wednesday, 9 January 2013 at 12:38:13 UTC, Jonathan M Davis wrote:
>>
>> When you append to array, the elements being added are in untyped
>> memory. So,
>> no mutation of immutable anything is going on at all.
> 
> Ok, I guess that makes sense.
> 

Newbie answer and question to follow.

The area of memory after .length to .capacity is not defined (untyped) in 
the D spec.  It is a trick used by the compiler to not slow down appends 
with repetitive memory allocations.  I believe that since the memory has 
been allocated for an array of a certain type, the extra capacity memory 
should be of that type until the array and its slices have been 
deallocated by druntime.

My question is about immutable and casting in general.  I've heard Walter 
say of immutable, "Turtles all the way down.".  If say I have a plug-in  
system with a defined interface and I pass it an immutable array (ie 
immutable(int)[]) then it uses a cast to mutate, that seems to defeat the 
purpose of immutable.  When the plug-in returns, I now have an immutable 
array that has been mutated.  Is this how immutable and casting are 
supposed to work?  


More information about the Digitalmars-d-learn mailing list