I don't like slices in D

Ali Çehreli acehreli at yahoo.com
Thu Oct 17 20:45:18 PDT 2013


On 10/17/2013 03:33 PM, Sean Kelly wrote:

 > I'd be curious to see if this would ever relocate:
 >
 > int[] dArr = [10,11,12];
 > const(int)[] dSlice = dArr[0..2];
 > dSlice.length++;
 >
 > It shouldn't, since growing a const slice can never clobber the
 > underlying array,

However, according to spec, the appended elements must be 0. There is an 
optimization opportunity if the elements beyond dSlice's end were all 
zeros and if the type system guaranteed that they were immutable. Only 
then dSlice's relocation could be elided.

 > but I don't know if the current implementation handles
 > this case.  Still, growing a const slice seems like a pretty useless
 > thing to do, since you can only expect to end up with default values at
 > the end.

Still, that may be desired to avoid special-casing the elements at the 
end. It would be simpler to append zeros and just use them.

Ali



More information about the Digitalmars-d mailing list