I don't like slices in D
Adam D. Ruppe
destructionator at gmail.com
Fri Oct 18 07:27:35 PDT 2013
On Friday, 18 October 2013 at 13:38:01 UTC, Vitali wrote:
> My prevoius example of the function "removeElement(ref int[],
> int)" will not work. The right implementation would be:
> void removeElement(ref int[] arr, int index) {
> arr[index..$-1] = arr[index+1..$].dup;
.dup allocates a copy. The right implementation would be to copy
the data yourself, then reduce length, and then just leave it at
that.
void removeElement(ref int[] arr, int index) {
for(size_t idx = index; idx < arr.length - 1; idx++)
arr[idx] = arr[idx + 1];
arr.length = arr.length - 1; // arr.length-- won't compile
due to silliness
}
More information about the Digitalmars-d
mailing list