array slicing policy

Tim M tim.matthews7 at gmail.com
Thu Aug 6 06:53:17 PDT 2009


On Thu, 06 Aug 2009 04:13:07 -0400
HOSOKAWA Kenchi <hskwk at inter7.jp> wrote:

> Hello,
> 
> 
> D's array slicing is basically "an array means to specify a subarray
> of it.

Static arrays are the same as in C but I prefer to think of dynamic
arrays as a handy GC'd block of memory that comes with a free view and
any additional views can be obtained at any time. This is because all
views are equal including the original and there is not really any
concept of 'an array as a sub of another'.

> An array slice
> does not copy the data, it is only another
> reference to it."

b = a; //sets b to reference the same data that a gets
b[] = a[]; //copy a into the data block that b already points to (must
match lengh)
b = a.dup; //set b to reference a new copy of a (or use .idup for
immutable)


 How would you think of D's array slicing
> policy? I think that the policy may affect the design of other Range
> implementation because Range in foreach might be expected to be
> slice-like object.

I think Andrei has mostly worked out the fundamentals of ranges and it
is probably done by treating exisiting views as immutable.

To conclude it works if you can guarantee that you can have all views
remain valid after any changes to the data block they point too as they
are not automatically updated.

Also if the compiler sees an update to the length property it will
insert code to re size the memory block it points too as well as update
the length property. Apart from this compile time understanding of your
code, dynamic arrays are interfaced with nothing but a simple struct of
the length & pointer. This can be wrapped for extra security
(several implementations already exist) but many like to have the
maximum performance of it being lightweight.




More information about the Digitalmars-d mailing list