# A function like copyto! that acts recursively as deepcopy

**URL:** <https://discourse.julialang.org/t/a-function-like-copyto-that-acts-recursively-as-deepcopy/58027>\
**Category:** General Usage\
**Created:** [March 26, 2021, 3:43pm UTC](https://discourse.julialang.org/t/a-function-like-copyto-that-acts-recursively-as-deepcopy/58027 "2021-03-26T15:43:33Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![kellertuer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kellertuer/32/220707_2.png) [@kellertuer](https://discourse.julialang.org/u/kellertuer)\
**Post date:** [March 26, 2021, 3:43pm UTC](https://discourse.julialang.org/t/a-function-like-copyto-that-acts-recursively-as-deepcopy/58027/1 "2021-03-26T15:43:33Z")

</div>

Recently I stumbled upon a case where I for example have an array of arrays (`a`) and a second memory already allocated (`b`) – this might also be deeper nested than in the following example.

```julia
a = [[1],[2],[3]]
b = similar.(a)

```

Now if I want to copy the values and not the arrays, `copyto!` won’t work with

```julia
copyto!(b,a)
b[3][1] = 4

```

I would also change `a`. What’s the best way to copy the values (not references) of the inner arrays (recursively) as `deepcopy` would do? Note that `b` is allocated already, so deepcopy would allocate new memory here, which I would like to avoid.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [March 27, 2021, 4:25pm UTC](https://discourse.julialang.org/t/a-function-like-copyto-that-acts-recursively-as-deepcopy/58027/2 "2021-03-27T16:25:34Z")

</div>

Eg

```julia
map(copyto!, b, a)

```

but there are a lot of equivalent solutions (broadcasting, a loop, etc).

---

<div class="post-metadata">

**Author:** ![kellertuer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kellertuer/32/220707_2.png) [@kellertuer](https://discourse.julialang.org/u/kellertuer)\
**Post date:** [March 27, 2021, 4:36pm UTC](https://discourse.julialang.org/t/a-function-like-copyto-that-acts-recursively-as-deepcopy/58027/3 "2021-03-27T16:36:37Z")

</div>

Thanks.  
But this would just be one level of recursion, i.e. not work for arrays of arrays of arrays? (Similarly would `copyto!.(a,b)`, right?  
I would like a method that – as deep copy – does this on arbitrary levels. And sure, my MWE Is too small for that.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [March 27, 2021, 4:39pm UTC](https://discourse.julialang.org/t/a-function-like-copyto-that-acts-recursively-as-deepcopy/58027/4 "2021-03-27T16:39:47Z")

</div>

Something like

```julia
function recursive_copyto!(a::AbstractArray{T}, b::AbstractArray{T}) where {T}
    if T <: AbstractArray
        foreach(recursive_copyto!, a, b)
    else
        copyto!(a, b)
    end
    a
end

```

?

---

<div class="post-metadata">

**Author:** ![kellertuer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kellertuer/32/220707_2.png) [@kellertuer](https://discourse.julialang.org/u/kellertuer)\
**Post date:** [March 27, 2021, 4:49pm UTC](https://discourse.julialang.org/t/a-function-like-copyto-that-acts-recursively-as-deepcopy/58027/5 "2021-03-27T16:49:21Z")

</div>

That looks nice, maybe I would do that with dispatch?

```
function recursive_copyto!(a::AbstractArray{T}, b::AbstractArray{T}) where {T<:AbstractArray}
    foreach(recursive_copyto!, a, b)
    return a
end
function recursive_copyto!(a::AbstractArray{T}, b::AbstractArray{T}) where {T}
    copyto!(a, b)
end

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [March 27, 2021, 4:53pm UTC](https://discourse.julialang.org/t/a-function-like-copyto-that-acts-recursively-as-deepcopy/58027/6 "2021-03-27T16:53:30Z")

</div>

Sure, if you prefer doing the job of the compiler.

---

<div class="post-metadata">

**Author:** ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)\
**Post date:** [March 27, 2021, 4:56pm UTC](https://discourse.julialang.org/t/a-function-like-copyto-that-acts-recursively-as-deepcopy/58027/8 "2021-03-27T16:56:52Z")

</div>

Question. Given that arrays of arrays are in reality arrays of references to other arrays, and that they can change in size, etc.

When you do

```julia
a = [[1],[2],[[3,4,5]]]
b = similar.(a)

```

Are you allocating only enough space to get three pointers to other arrays, or are you really allocating the space for the three pointers and space for one Int associated with those pointers and then three ints in the inner array? I figure if your inside array has a more complicated layout I may become more difficult to really allocate everything at once?

The example above yields:

```julia
julia> a = [[1],[2],[[3,4,5]]]
3-element Vector{Vector{T} where T}:
 [1]
 [2]
 [[3, 4, 5]]

julia> b = similar.(a)
3-element Vector{Vector{T} where T}:
 [4831755376]
 [4660659088]
 [#undef]

```

What I wonder if is you really save something preallocating this instead of just doing a `deepcopy`

---

<div class="post-metadata">

**Author:** ![kellertuer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kellertuer/32/220707_2.png) [@kellertuer](https://discourse.julialang.org/u/kellertuer)\
**Post date:** [March 27, 2021, 5:04pm UTC](https://discourse.julialang.org/t/a-function-like-copyto-that-acts-recursively-as-deepcopy/58027/9 "2021-03-27T17:04:10Z")

</div>

For types I prefer to not write it wit an `if`, since I find the code cleaner. Is your code faster?

---

<div class="post-metadata">

**Author:** ![kellertuer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kellertuer/32/220707_2.png) [@kellertuer](https://discourse.julialang.org/u/kellertuer)\
**Post date:** [March 27, 2021, 5:08pm UTC](https://discourse.julialang.org/t/a-function-like-copyto-that-acts-recursively-as-deepcopy/58027/10 "2021-03-27T17:08:31Z")

</div>

Oh, the point is, that in the original code my `b` _is already_ initialised fully, since it comes from somewhere else, so there is no deep copy I would like to do, I really just want to copy the values. So there is no `similar.(...)` in my code. Initialilzation happens far before and I am sure both `a` and `b` are initialised and of same size, it actually is a point on the nested array [PowerManifold](https://juliamanifolds.github.io/Manifolds.jl/v0.1/manifolds/power.html#Manifolds.PowerManifold) (and maybe even a point on a power manifold _of_ a power manifold, so even 3 levels of arrays)

Sorry if my MWE caused confusion there.

---

<div class="post-metadata">

**Author:** ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)\
**Post date:** [March 27, 2021, 5:09pm UTC](https://discourse.julialang.org/t/a-function-like-copyto-that-acts-recursively-as-deepcopy/58027/11 "2021-03-27T17:09:44Z")

</div>

Oh, thanks for explaining and sorry for adding noise.

---

<div class="post-metadata">

**Author:** ![kellertuer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kellertuer/32/220707_2.png) [@kellertuer](https://discourse.julialang.org/u/kellertuer)\
**Post date:** [March 27, 2021, 5:19pm UTC](https://discourse.julialang.org/t/a-function-like-copyto-that-acts-recursively-as-deepcopy/58027/12 "2021-03-27T17:19:46Z")

</div>

No problem – actually we also solve this recursive `similar` problem you stumbled upon, which is (similar as asked here) just recursively applying similar, we call it `allocate` in our package, see [https://juliamanifolds.github.io/Manifolds.jl/latest/interface.html#Allocation](https://juliamanifolds.github.io/Manifolds.jl/latest/interface.html#Allocation).

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [March 27, 2021, 6:44pm UTC](https://discourse.julialang.org/t/a-function-like-copyto-that-acts-recursively-as-deepcopy/58027/13 "2021-03-27T18:44:09Z")

</div>

No, there should be no speed difference. What you find cleaner is of course a matter of taste, just do it the way you prefer.

---

<div class="post-metadata">

**Author:** ![kellertuer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kellertuer/32/220707_2.png) [@kellertuer](https://discourse.julialang.org/u/kellertuer)\
**Post date:** [March 27, 2021, 6:46pm UTC](https://discourse.julialang.org/t/a-function-like-copyto-that-acts-recursively-as-deepcopy/58027/14 "2021-03-27T18:46:00Z")

</div>

Thanks for the clarification. Sure that is just a matter of taste. That’s why I also marked your answer as the solution. Thanks for the help
