resizeable arrays: T[new]

gareis dhasenan at gmail.com
Mon Jun 4 21:09:02 PDT 2007


Derek Parnell wrote:
> On Mon, 04 Jun 2007 10:45:09 -0700, Walter Bright wrote:
> 
>>>>    T[n]   a;  // static array
>>>>    T[]    b;  // dynamic array
>>>>    T[new] c;  // resizeable array
>>> I'd propose a different nomenclature:
>>>
>>> T[n]   a;  // static array
>>> T[]    b;  // (array) slice
>>> T[new] c;  // dynamic array
>> I like "resizeable" array because it is pretty clear what it does.
> 
> I think you are quite wrong Walter. The new interpretation of 'x[]' is
> almost identical to what we used to think of as a slice. And the
> interpreatation of 'x[new]' is exactly what used to be known as a dynamic
> array. 
> 
> Your change will effect all code immediately whereas reversing the
> (changed) meaning of '[]' and '[new]' to mean 'resizable' and 'slice' will
> allow coders to change their coding over time. Unless of course that's what
> you are trying to do ...
> 
> 

There's another issue. Function parameters cover most of the ground that 
this does, if not more. There's already a set of keywords that can be 
used here with the same meaning.

C++ has T const * as a single-allocate array and T* as the resizeable 
form (albeit only with realloc). It'd be a bit ugly, but D could do T 
const[] or T const([]) for the single-allocate array and T[] for the 
resizeable form.

T const[] also addresses the above issue of converting code. I'm working 
on a MUD currently, and since it's a text-based game just about 
everything involved is string catenation. Either I'd suddenly grow to 
love the auto keyword (but specifying types manually is a comforting 
sanity check), or I'd have to convert almost every string to be mutable.

Walter, you said already that you couldn't think of many cases where it 
would be useful to have an immutable array of mutable elements. But now 
it seems that not only are you implementing that, but you're making it 
the default. I'm confused. Why are you doing it?

I agree it's useful to have these slice arrays, but it's an edge case. 
Please don't require extra keywords to get to the common case.



More information about the Digitalmars-d-announce mailing list