[phobos] Array preallocate function

Andrei Alexandrescu andrei at erdani.com
Tue Feb 23 10:35:31 PST 2010


Steve Schveighoffer wrote:
> If someone has an array, and they want to trim one element off the
> end, it would be feasible they may write:
> 
> array.length -= 1;
> 
> vs the more verbose
> 
> array = array[0..$-1];
> 
> Letting these two statements do something different is a mistake in
> my mind.

Agreed.

> What about a "minimize" function, which simply truncates any
> "allocated" length after an array.  So you would reset an array via:
> 
> arr.length = 0; arr.minimize();
> 
> The advantage here is the array's length is not affected, just the
> allocated length is reduced to match the array's length.  There are
> less invalid cases to worry about (i.e. "shrinking" to something
> larger doesn't make any sense).

Sounds good. I'd choose a more specific name, e.g. shrinkToFit. Minimize 
has me think of optimization functions.

Andrei


More information about the phobos mailing list