Thread overview
array.length as an l value
Nov 24, 2004
Kevin Watters
Nov 25, 2004
Sean Kelly
Nov 25, 2004
Ilya Minkov
Nov 28, 2004
Simon Buchan
Nov 28, 2004
Ben Hinkle
November 24, 2004
Just a quirk...

Is there any reason you can do

array.length = array.length - 1;

..but not

array.length--; // ?


November 25, 2004
Kevin Watters wrote:
> Just a quirk...
> 
> Is there any reason you can do
> 
> array.length = array.length - 1;
> 
> ..but not
> 
> array.length--; // ?

I think to make it less simple to produce bad code such as this:

array[--length*2] = x;


Sean
November 25, 2004
Kevin Watters schrieb:
> Just a quirk...
> 
> Is there any reason you can do

Because resizing an array is a very costy operation in D and you really don't want to do that for each element. Resizing is more costy than, say, in STL, because ownage and memory management are done differently from, say, STL. Keywords: Garbage Collection, Copy-on-Write convention.

As to reducing the array length... I'm not sure now, but i think it uses slice, not copy, so should incure almost no cost at all - as oppsed to increasing it. But i'm not sure it is worth an exception from the rules.

-eye
November 28, 2004
On Thu, 25 Nov 2004 17:00:57 +0100, Ilya Minkov <minkov@cs.tum.edu> wrote:

> Kevin Watters schrieb:
>> Just a quirk...
>>  Is there any reason you can do
>
> Because resizing an array is a very costy operation in D and you really don't want to do that for each element. Resizing is more costy than, say, in STL, because ownage and memory management are done differently from, say, STL. Keywords: Garbage Collection, Copy-on-Write convention.
>
> As to reducing the array length... I'm not sure now, but i think it uses slice, not copy, so should incure almost no cost at all - as oppsed to increasing it. But i'm not sure it is worth an exception from the rules.
>
> -eye

Well its the same for increment and all the assign operators as well,
(*=, etc...) so its not an exception.

AFAIK, decreasing the length only tells the GC that it can use the memory
after the end now, increasing an array the first time moves it to GC
controlled mem (as opposed to the stack?) and afterwards only moves it if
increasing the length would hit already alloc'ed mem. (This is all
implentation dependant stuff, of course)

If so, its only _occasionally_ expensive... (But that is a big If)

Perhaps a .reserved array property? But that would be part of the GC, not the
array... I don't know the answer, but I do know that it should be a damn sight
easier to tell D you want to add one element.

-- 
Using Opera's revolutionary e-mail client: http://www.opera.com/m2/
November 28, 2004
[snip]
> Perhaps a .reserved array property?

That would be nice. MinTL has a "reserve" template that reserves space for dynamic or associative arrays but it is limited to non-zero length arrays and it doesn't return the current amount of available space.

> But that would be part of the GC, not  the
> array... I don't know the answer, but I do know that it should be a damn
> sight
> easier to tell D you want to add one element.

Usually one knows what element to add and so ~= is the way to go.