Deep copy or clone data structure (or object ?)

Daniel Davidson nospam at spam.com
Mon Dec 2 11:46:50 PST 2013


On Monday, 2 December 2013 at 13:42:48 UTC, Dfr wrote:
> Hi
>
> I searched through various D documentation sources and did not 
> found anything except 'std.copy', but it's only for slices.
> Is there such feature in standart library ? Or some easy way to 
> clone for example map of slices of maps or an object with few 
> structs inside  ?

I rolled my own generalized dup: 
https://github.com/patefacio/d-help/blob/master/d-help/opmix/dup.d

I have since changed my philosophy to allow sharing more often 
than not - but it can be scary still so dup has advantages.

Here is an example usage. I hope one day something similar makes 
it into phobos.

import std.stdio;
import opmix.dup;

void main() {

   auto x = [
             "foo" : [ 1,2,3.3 ],
             "moo" : [ 1,2,3.2 ],
              ];

   auto y = x.gdup;
   writeln(y, " and ", x);
   x["foo"][0]++;
   writeln(y, " and ", x);
}
-------------------------------
["moo":[1, 2, 3.2], "foo":[1, 2, 3.3]] and ["foo":[1, 2, 3.3], 
"moo":[1, 2, 3.2]]
["moo":[1, 2, 3.2], "foo":[1, 2, 3.3]] and ["foo":[2, 2, 3.3], 
"moo":[1, 2, 3.2]]


More information about the Digitalmars-d-learn mailing list