resizeable arrays: T[new]

Bill Baxter dnewsgroup at billbaxter.com
Mon Jun 4 19:56:53 PDT 2007


Ary Manzana wrote:
> Walter Bright escribió:
>>
>>    T[n]   a;  // static array
>>    T[]    b;  // dynamic array
>>    T[new] c;  // resizeable array
>>
>>    a.length = 6;   // error, static array
>>    b.length = 6;   // error, dynamic array is not resizeable
>>    c.length = 6;   // ok
>>    b = c;          // ok, implicit conversion of resizeable to dynamic
>>    c = b;          // error
>>    c = b[0..n];    // error
>>    b = c[0..n];    // ok, but watch out if c is later resized
>>    b = c[0..n].dup; // ok, no worries
>>    c = cast(T[new])b; // ok, but this will be deliberate
> 
> I don't understand how dynamic arrays work with this change.
> 
> T[] b;        // b is a dynamic array of size... ???
> b.length = 6; // error, dynamic array is not resizeable ????????
> 
> So maybe this should be:
> 
> T[] b = new T[6]; // ... of size 6, and 6 can
>                   // be replaced with a dynamic variable
> 
> Is this the intention of it?

Yep, I'm pretty sure that's what you'll have to do for a 
*non*-resizeable array.  On the other hand if you want to be able to 
resize (using Walter's not-so-popular syntax):

T[new] b;
b.length = 6;

> I've only wrote one program in D, and I used dynamic arrays as lists 
> (AKA ArrayList in Java). So I guess my program will break with this 
> change, because I can't concatenate anymore. Maybe I've been using 
> dynamic arrays in the wrong way?  

Nope, the way you've been using them is fine.  But to get safer slices, 
something's got to change.  And since not resizing is less common than 
resizing, it makes more sense to change the syntax for the resizeable 
arrays.

--bb



More information about the Digitalmars-d-announce mailing list