I don't like slices in D
Meta
jared771 at gmail.com
Thu Oct 17 12:08:25 PDT 2013
On Thursday, 17 October 2013 at 18:51:08 UTC, Meta wrote:
> As David Eagen said, your problem is that in D, dArr[0..2] is
> not inclusive, it's exclusive, so you get dArr[0] and dArr[1].
> Changing it to dSlice = dArr[0..3] will work (or better,
> dArr[0..$]). However, there's something else going on here
> that's fishy:
>
> void testSlices()
> {
> int[] dArr = [10, 11, 12];
> int[] dSlice = dArr[0..2];
> writeln(dArr.ptr, " ", dArr.capacity, " ", dArr.length);
> writeln(dSlice.ptr, " ", dSlice.capacity, " ", dSlice.length);
>
> dSlice.length++;
> writeln(dSlice.ptr, " ", dSlice.capacity, " ", dSlice.length);
>
> writeln(dArr);
> writeln(dSlice);
> }
>
> 4002DFF0 3 3
> 4002DFF0 0 2
> 4002DFE0 3 3
> [10, 11, 12]
> [10, 11, 0]
>
> dSlice says that it has length 2, but accessing dSlice[2] does
> not produce a RangeError... Likewise, printing it out prints 3
> elements, not 2. This looks like a bug to me.
Never mind, that was extremely silly of me.
More information about the Digitalmars-d
mailing list