cloning array
Steven Schveighoffer
schveiguy at gmail.com
Wed Jun 2 16:37:23 UTC 2021
On 6/2/21 11:32 AM, Sean wrote:
> Is this normal behavior of dup? if so, how can I get the behavior i am
> searching for? Thank you.
>
Yes. `dup` is a shallow copy.
To get the behavior you want:
```d
auto deepdup(T)(T[] arr)
{
import std.algorithm, std.array;
static if(is(T == U[], U))
return arr.map!(x => .deepdup(x)).array;
else
return arr.dup;
}
// use like arr.deepdup where you would normally put arr.dup
```
Note that this is going to be pretty inefficient allocation-wise, but
will work with minimal code.
-Steve
More information about the Digitalmars-d-learn
mailing list