www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Deep copy or clone data structure (or object ?)

reply "Dfr" <deflexor yandex.ru> writes:
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  ?
Dec 02 2013
next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
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
Dec 02 2013
prev sibling parent "Daniel Davidson" <nospam spam.com> writes:
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]]
Dec 02 2013