Why you might avoid `deepcopy` in Julia

By: Julia on EPH

Re-posted from: https://ericphanson.com/blog/2024/why-you-might-avoid-deepcopy-in-julia/

Why use deepcopy? In Julia, copy is a function which creates a shallow copy. For example:
julia> a = [1] # vector with one element, namely 1 1-element Vector{Int64}: 1 julia> b = [a] # vector with one element, `a` 1-element Vector{Vector{Int64}}: [1] julia> b2 = copy(b) # new vector, also with one element which is `a` 1-element Vector{Vector{Int64}}: [1] julia> push!(a, 2) # mutate `a` so it contains 1 and 2 2-element Vector{Int64}: 1 2 julia> b # since `b` contains `a`, we can see its (nested) contents have changed 1-element Vector{Vector{Int64}}: [1, 2] julia> b2 # same for `b2`!