T[new]

Jarrett Billingsley jarrett.billingsley at gmail.com
Mon Aug 10 09:11:34 PDT 2009


On Sun, Aug 9, 2009 at 4:29 PM, Walter Bright<newshound1 at digitalmars.com> wrote:
> D has a number of subtle problems (performance and semantic) that arise when
> arrays are resized. The solution is to separate resizeable array types from
> slices. Slices will retain the old:
>
>   T[] slice;
>
> syntax. Resizeable arrays will be declared as:
>
>   T[new] array;
>
> The new expression:
>
>   new T[10]
>
> will return a T[new].
>
> T[new] will implicitly convert to T[], but not the other way.
>
> slice.length will become read-only.
>
> Under the hood, a T[new] will be a single pointer to a library defined type.
> This library defined type will likely contain three properties:
>
>    size_t length;
>    T* ptr;
>    size_t capacity;
>
> The usual array operations will work on T[new] as well as T[].
>
> Doing this change will:
>
> 1. fix many nasties at the edges of array semantics
>
> 2. make arrays implementable on .net
>
> 3. make clear in function signatures if the function can resize the array or
> not

This is a great change.  MiniD's array semantics are very similar to
D's wrt. slicing, though I made the slight modification that array
objects keep track of whether or not they are a slice of another array
and behave slightly differently accordingly, and I've found it to be
more intuitive.  I like that D is doing the same thing, and statically
too ;)

The T[new] syntax isn't doing much for me, but then again, I don't
have any better suggestions.



More information about the Digitalmars-d mailing list