Why .dup not work with multidimensional arrays?

Dennis Ritchie via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu May 7 19:15:37 PDT 2015


Hi,
Should the method .dup work with multidimensional arrays for 
copying?

-----
import std.stdio;

void main() {

	auto a = [1, 2, 3];
	auto b = a.dup;

	b[] *= 2;
	writeln("a = ", a); // [1, 2, 3] // OK
	writeln("b = ", b); // [2, 4, 6] // OK


	auto c = [[[1, 2, 3], [4, 5, 6, 7, 8]],
		  [[9, 10], [11, 12, 13]]];

	auto d = c.dup;

	writeln("d[0][1][1 .. $ - 1] = ",
		 d[0][1][1 .. $ - 1]);

	d[0][1][1 .. $ - 1] *= 3;

	writeln("c = ", c);
	// [[[1, 2, 3], [4, 15, 18, 21, 8]],
	//  [[9, 10], [11, 12, 13]]] // wrong
	writeln("d = ", d);
	// [[[1, 2, 3], [4, 15, 18, 21, 8]],
	//  [[9, 10], [11, 12, 13]]] // OK
}
-----
http://ideone.com/Ddtm47

I thought the slice of the array c[0][1][1 .. $ - 1] = [5, 6, 7] 
not had to change to [15, 18, 21] by multiplying by 3.


More information about the Digitalmars-d-learn mailing list