Inability to dup/~ for const arrays of class objects

Steven Schveighoffer schveiguy at yahoo.com
Thu May 30 10:09:02 PDT 2013


On Thu, 30 May 2013 12:56:46 -0400, Steven Schveighoffer  
<schveiguy at yahoo.com> wrote:

> On Thu, 30 May 2013 12:46:39 -0400, Maxim Fomin <maxim at maxim-fomin.ru>  
> wrote:
>
>> Please provide reasons why it is wrong (but without explanation how  
>> druntime allocates memory which is irrelevant).
>
> It's wrong in that D's spec re-defines dynamic arrays from the  
> traditional definition (I think for the sake of simplicity, but I didn't  
> write the definition, so I'm not sure).  D's slices aren't dynamic  
> arrays, no matter how many specs say so.

Oh, you were looking for an actual *functional* differences between slices  
and dynamic arrays.

The major concept difference is ownership.  Two references to the same  
array do not mysteriously split when length is extended on one of those  
references.  All references to the same array always refer to the same  
array, even when it has to reallocate to extend length.  Slices differ  
 from this behavior, and I can say with great confidence that this is the  
most confusing aspect of D slices.

In addition, shrinking a dynamic array invalidates any data that has now  
been removed.  D slices do not do this either.  Because slices simply  
point at data, they don't own it.

-Steve


More information about the Digitalmars-d mailing list