When a slice is assigned by a scalar, the value of the assignment expression has the length of the whole array, not that of the slice.
----------
import std.stdio;
void show(int[] s) {
foreach (int i; s) {
writef("%d ", i);
}
writefln();
}
void main() {
int[] qwert = new int[6];
int[] yuiop;
yuiop = qwert[2..5] = 3;
show(yuiop);
show(qwert[2..5] = 4);
show(qwert[2..5]);
show(qwert);
show(yuiop[2..5] = qwert[1..4]);
yuiop = qwert[2..5];
show(yuiop[1..3] = 6);
writefln((yuiop[1..3] = 7).length);
}
----------
Output:
3 3 3 0 0 0
4 4 4 0 0 0
4 4 4
0 0 4 4 4 0
0 4 4
6 6 4
3
Expected output:
3 3 3
4 4 4
4 4 4
0 0 4 4 4 0
0 4 4
6 6
2
A testcase (array_chain.d) is also included in my DStress contribution apparently still waiting to be added (see bug 63).
Comment #1 by matti.niemenmaa+dbugzilla — 2006-04-03T06:00:11Z
Fixed in 0.151.
Also, unless I'm mistaken, there is a bug in that test code: you have "show(yuiop[2..5] = qwert[1..4]);" at a point where yuiop's length is 3. Shouldn't that read "show(yuiop = qwert[1..4]);"? I get your expected output after that change.
Comment #2 by smjg — 2006-04-04T06:22:32Z
You're right that the testcase is buggy. However, the point was to compare the behaviour when copying a slice to a slice, whereas your proposed change turns it into a reference assignment.
A better corretion is simply to add
yuiop.length = 6;
before that line.