cloning array

Alexandru Ermicioi alexandru.ermicioi at gmail.com
Thu Jun 3 15:06:00 UTC 2021


On Wednesday, 2 June 2021 at 15:32:38 UTC, Sean wrote:
> ...

You can implement deep copy using template recursion:

     import std;

     T[] rdup(T : U[], U)(T[] duped) {
         return duped.map!(arr => arr.rdup).array;
     }

     T[] rdup(T)(T[] duped) {
         return duped.dup;
     }

     void main()
     {
         int[][][] dupeable = [[[1], [2]], [[3]]];
         auto duped = dupeable.rdup;
         duped[0][0][0] = 9;
         writeln("Hello D-eep copy of ", dupeable, " to ", duped);
     }

Best regards,
Alexandru.


More information about the Digitalmars-d-learn mailing list