www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - changing array lengths

reply Ellery Newcomer <ellery-newcomer utulsa.edu> writes:
Does the following totally not make sense? I think it should work.

int[] a = new int[10];

a.length += 30;
Dec 09 2008
next sibling parent "Jarrett Billingsley" <jarrett.billingsley gmail.com> writes:
On Tue, Dec 9, 2008 at 11:54 PM, Ellery Newcomer
<ellery-newcomer utulsa.edu> wrote:
 Does the following totally not make sense? I think it should work.

 int[] a = new int[10];

 a.length += 30;
It's a weird limitation that's been around forever. Ugh. Here, have this. T[] resize(T)(ref T[] arr, ptrdiff_t delta) { arr.length = arr.length + delta; return arr; } ... auto a = new int[10]; a.resize(30); // now 40 a.resize(-30); // now 10
Dec 09 2008
prev sibling parent Derek Parnell <derek psych.ward> writes:
On Tue, 09 Dec 2008 22:54:30 -0600, Ellery Newcomer wrote:

 Does the following totally not make sense? I think it should work.
 
 int[] a = new int[10];
 
 a.length += 30;
Unfortunately, the .length property, like all properties, do not support the 'op=' syntax. You have to do it the long way ... a.length = a.length + 30; -- Derek Parnell Melbourne, Australia skype: derek.j.parnell
Dec 10 2008