# Deepsimilar

**URL:** <https://discourse.julialang.org/t/deepsimilar/2209>\
**Category:** Internals & Design\
**Tags:** proposal\
**Created:** [February 21, 2017, 10:51am UTC](https://discourse.julialang.org/t/deepsimilar/2209 "2017-02-21T10:51:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lostella](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lostella/32/356_2.png) [@lostella](https://discourse.julialang.org/u/lostella)\
**Post date:** [February 21, 2017, 10:51am UTC](https://discourse.julialang.org/t/deepsimilar/2209/1 "2017-02-21T10:51:23Z")

</div>

Wouldn’t it make sense to have a `deepsimilar` function shipped with Julia, in the same way `deepcopy` is? For example, for nested array-like objects containing numbers, this could be defined as

```julia
function deepsimilar(x::AbstractArray)
	y = similar(x)
	for k in eachindex(x)
		y[k] = similar(x[k])
	end
	return y
end

deepsimilar{T <: Number}(x::AbstractArray{T}) = similar(x)

```

I guess for generalizing this I should look into how `deepcopy` is done.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [February 24, 2017, 8:07pm UTC](https://discourse.julialang.org/t/deepsimilar/2209/2 "2017-02-24T20:07:00Z")

</div>

What’s it for?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [February 24, 2017, 8:45pm UTC](https://discourse.julialang.org/t/deepsimilar/2209/3 "2017-02-24T20:45:35Z")

</div>

It’s nice for nested arrays. Another related case is recursive `copy!` operations, again because `copy!` on `Vector{Vector}` will copy over the references and not the values.

I have a small library, RecursiveArrayTools.jl, for holding these kinds of routines because I need them quite often for DiffEq stuff.

[https://github.com/ChrisRackauckas/RecursiveArrayTools.jl](https://github.com/ChrisRackauckas/RecursiveArrayTools.jl)

I think `deepsimilar` would do well there, but I would like to see a group of tools dedicated to this in Base since I was surprised I couldn’t find a simple way to do this.

---

<div class="post-metadata">

**Author:** ![lostella](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lostella/32/356_2.png) [@lostella](https://discourse.julialang.org/u/lostella)\
**Post date:** [February 25, 2017, 10:55am UTC](https://discourse.julialang.org/t/deepsimilar/2209/4 "2017-02-25T10:55:57Z")

</div>

Well, I found myself implementing this in order to be able to represent blocks of variables using `Array{Array}`. I use `deepsimilar` to instantiate “copies” (i.e. arrays with the same shape, and whose elements have the same shape, and so on) on which to perform operations, without actually copying the data. I guess when you don’t need to replicate data into the new object, `similar`-ing it is faster than `copy`-ing it.

It might make sense also for more structured types other than nested arrays.
