Re: Why .dup not work with multidimensional arrays?

2015-05-08 Thread Ali Çehreli via Digitalmars-d-learn
On 05/07/2015 07:39 PM, Dennis Ritchie wrote: On Friday, 8 May 2015 at 02:23:23 UTC, E.S. Quinn wrote: It's because arrays are references types, and .dup is a strictly shallow copy, so you're getting two outer arrays that reference the same set of inner arrays. You'll have to duplicated each of

Re: Why .dup not work with multidimensional arrays?

2015-05-08 Thread Dennis Ritchie via Digitalmars-d-learn
On Friday, 8 May 2015 at 06:30:46 UTC, Ali Çehreli wrote: In D, everything is possible and very easy. :p I called it deepDup: import std.stdio; import std.traits; import std.range; import std.algorithm; auto deepDup(A)(A arr) if (isArray!A) { static if (isArray!(ElementType!A)) {

Re: Why .dup not work with multidimensional arrays?

2015-05-08 Thread Dennis Ritchie via Digitalmars-d-learn
On Friday, 8 May 2015 at 15:13:14 UTC, Ali Çehreli wrote: On 05/08/2015 08:05 AM, Dennis Ritchie wrote: why static int idx variable declared within a function deepDup takes the values 1, 1, 1, 2, 2, 3, 4, as opposed to a global variable static int idx, which receives the expected value of 1,

Re: Why .dup not work with multidimensional arrays?

2015-05-08 Thread Ali Çehreli via Digitalmars-d-learn
On 05/08/2015 08:05 AM, Dennis Ritchie wrote: why static int idx variable declared within a function deepDup takes the values 1, 1, 1, 2, 2, 3, 4, as opposed to a global variable static int idx, which receives the expected value of 1, 2, 3, 4, 5, 6, 7 ? That's because every template

Re: Why .dup not work with multidimensional arrays?

2015-05-08 Thread Chris via Digitalmars-d-learn
On Friday, 8 May 2015 at 06:30:46 UTC, Ali Çehreli wrote: On 05/07/2015 07:39 PM, Dennis Ritchie wrote: On Friday, 8 May 2015 at 02:23:23 UTC, E.S. Quinn wrote: It's because arrays are references types, and .dup is a strictly shallow copy, so you're getting two outer arrays that reference the

Why .dup not work with multidimensional arrays?

2015-05-07 Thread Dennis Ritchie via Digitalmars-d-learn
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 =

Re: Why .dup not work with multidimensional arrays?

2015-05-07 Thread Dennis Ritchie via Digitalmars-d-learn
On Friday, 8 May 2015 at 02:23:23 UTC, E.S. Quinn wrote: It's because arrays are references types, and .dup is a strictly shallow copy, so you're getting two outer arrays that reference the same set of inner arrays. You'll have to duplicated each of the inner arrays yourself if you need to make

Re: Why .dup not work with multidimensional arrays?

2015-05-07 Thread E.S. Quinn via Digitalmars-d-learn
It's because arrays are references types, and .dup is a strictly shallow copy, so you're getting two outer arrays that reference the same set of inner arrays. You'll have to duplicated each of the inner arrays yourself if you need to make a deep copy. On Friday, 8 May 2015 at 02:15:38 UTC,