Thread overview
Deep copy or clone data structure (or object ?)
Dec 02, 2013
Dfr
Dec 02, 2013
bearophile
Dec 02, 2013
Daniel Davidson
December 02, 2013
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  ?
December 02, 2013
Dfr:

> 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 think such functionality is not yet present in Phobos, probably because it's not a common need. What is your use case?

Bye,
bearophile
December 02, 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]]