digitalmars.D.learn - Deep copy or clone data structure (or object ?)
- Dfr (6/6) Dec 02 2013 Hi
- bearophile (5/10) Dec 02 2013 I think such functionality is not yet present in Phobos, probably
- Daniel Davidson (24/30) Dec 02 2013 I rolled my own generalized dup:
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
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
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