array slicing policy

HOSOKAWA Kenchi hskwk at inter7.jp
Thu Aug 6 01:13:07 PDT 2009


Hello,


D's array slicing is basically "an array means to specify a subarray of it. An array slice does not copy the data, it is only another reference to it."
This definition, however, is easily violated by instance reallocation such as changing its length:

int[] a = [0, 1, 2];
int[] b = a[0..2];
writeln(a.ptr, " ", b.ptr);
assert(a[0]==b[0]);

a.length = 2;
b[0] = -5;
writeln(a.ptr, " ", b.ptr);
assert(a[0]==b[0]);

a.length = 1000;
b[0] = int.min;
writeln(a.ptr, " ", b.ptr);
assert(a[0]==b[0]); // might fail


I won't be annoyed by this violation (with coding carefully) but it is only my own impression.
How would you think of D's array slicing policy?
I think that the policy may affect the design of other Range implementation
because Range in foreach might be expected to be slice-like object.



More information about the Digitalmars-d mailing list