# \#deepcopy

**URL:** https://discourse.julialang.org/tag/deepcopy/609.md

[Latest](https://discourse.julialang.org/latest.md) · [Categories](https://discourse.julialang.org/categories.md) · [Tags](https://discourse.julialang.org/tags.md)

---

## [Which copy function should I overload?](https://discourse.julialang.org/t/which-copy-function-should-i-overload/129595)

<div class="topic-metadata">

**Author:** [@mhauru](https://discourse.julialang.org/u/mhauru)\
**Replies:** 9\
**Last updated:** [June 5, 2025, 7:42am UTC](https://discourse.julialang.org/t/which-copy-function-should-i-overload/129595 "2025-06-05T07:42:26Z")

</div>

I have an abstract type, call it Abe, subtypes of which may or may not be mutable. I want a function that I can call on Abes that will make a (deepish) copy of the ones that are mutable (or in some cases wrap objects tha…

---

## [Copy on write?](https://discourse.julialang.org/t/copy-on-write/126021)

<div class="topic-metadata">

**Author:** [@iago-lito](https://discourse.julialang.org/u/iago-lito)\
**Replies:** 2\
**Last updated:** [February 18, 2025, 5:19pm UTC](https://discourse.julialang.org/t/copy-on-write/126021 "2025-02-18T17:19:17Z")

</div>

Hello Julia, I need copy-on-write behaviour to protect fields of the large values I’m handing out to my users from cluttering their memory with useless copies. Is there such thing as a julia Cow{T} akin to rust’s Cow\<T\>…

---

## [When should I define my own \`deepcopy\` and \`copy\` functions for types, if ever?](https://discourse.julialang.org/t/when-should-i-define-my-own-deepcopy-and-copy-functions-for-types-if-ever/123447)

<div class="topic-metadata">

**Author:** [@world-peace](https://discourse.julialang.org/u/world-peace)\
**Replies:** 10\
**Last updated:** [December 4, 2024, 3:24pm UTC](https://discourse.julialang.org/t/when-should-i-define-my-own-deepcopy-and-copy-functions-for-types-if-ever/123447 "2024-12-04T15:24:52Z")

</div>

Just wanted to understand if I should define deepcopy and copy for my own types, if at all. Looking at the code suggests to me deepcopy is automatic, and therefore there is generally no need to define it for user define…

---

## [How exactly is deepcopy defined?](https://discourse.julialang.org/t/how-exactly-is-deepcopy-defined/6445)

<div class="topic-metadata">

**Author:** [@Stephen\_Vavasis](https://discourse.julialang.org/u/Stephen_Vavasis)\
**Replies:** 12\
**Last updated:** [December 4, 2024, 10:58am UTC](https://discourse.julialang.org/t/how-exactly-is-deepcopy-defined/6445 "2024-12-04T10:58:47Z")

</div>

Observe the following: julia\> a = \[8,7,6\] 3-element Array{Int64,1}: 8 7 6 julia\> b = \[a,a\] 2-element Array{Array{Int64,1},1}: \[8, 7, 6\] \[8, 7, 6\] julia\> c=deepcopy(b) 2-element Array{Array{Int64,1},1}: \[8, 7, 6\] …

---

## [How is \`deepcopy\` so clever regarding aliasing and self-reference?](https://discourse.julialang.org/t/how-is-deepcopy-so-clever-regarding-aliasing-and-self-reference/113235)

<div class="topic-metadata">

**Author:** [@iago-lito](https://discourse.julialang.org/u/iago-lito)\
**Replies:** 4\
**Last updated:** [April 19, 2024, 1:43pm UTC](https://discourse.julialang.org/t/how-is-deepcopy-so-clever-regarding-aliasing-and-self-reference/113235 "2024-04-19T13:43:10Z")

</div>

I find this behaviour very fortunate, but also black magic: a = \[5\] u = \[a, a, a\] v = deepcopy(u) v\[1\]\[1\] = 8 v # \[\[8\], \[8\], \[8\]\] # My naive 'deepcopy' would have produced \[\[8\], \[5\], \[5\]\] instead. I expected deepcopy …

---

## [Why can I \`deepcopy(nothing)\` but not \`copy(nothing)\`?](https://discourse.julialang.org/t/why-can-i-deepcopy-nothing-but-not-copy-nothing/112429)

<div class="topic-metadata">

**Author:** [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Replies:** 5\
**Last updated:** [April 3, 2024, 6:11am UTC](https://discourse.julialang.org/t/why-can-i-deepcopy-nothing-but-not-copy-nothing/112429 "2024-04-03T06:11:47Z")

</div>

I know it sounds silly but it is currently blocking an AD package of mine ^^ julia\> deepcopy(nothing) julia\> copy(nothing) ERROR: MethodError: no method matching copy(::Nothing)

---

## [Deepcopy dangers and drawbacks for nested data structures](https://discourse.julialang.org/t/deepcopy-dangers-and-drawbacks-for-nested-data-structures/111998)

<div class="topic-metadata">

**Author:** [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Replies:** 9\
**Last updated:** [March 24, 2024, 10:10pm UTC](https://discourse.julialang.org/t/deepcopy-dangers-and-drawbacks-for-nested-data-structures/111998 "2024-03-24T22:10:59Z")

</div>

Since reading this issue: Should \`copy\` be renamed \`shallowcopy\`? · Issue #42796 · JuliaLang/julia · GitHub I have always avoided using deepcopy. Now I have a situation in which to define custom copy methods for my type…

---

## [Deepcopy() of Dict has same mallocs as deepcopy() of Vector inside mutable Struct](https://discourse.julialang.org/t/deepcopy-of-dict-has-same-mallocs-as-deepcopy-of-vector-inside-mutable-struct/53420)

<div class="topic-metadata">

**Author:** [@bsuwal](https://discourse.julialang.org/u/bsuwal)\
**Replies:** 1\
**Last updated:** [March 23, 2024, 11:03pm UTC](https://discourse.julialang.org/t/deepcopy-of-dict-has-same-mallocs-as-deepcopy-of-vector-inside-mutable-struct/53420 "2024-03-23T23:03:22Z")

</div>

I had a struct that had a Dict as one of its fields. I deepcopy the struct objects a lot, and that was the bottleneck in the program. I then realized that I could replace the Dict with a Vector, which I know has a smalle…

---

## [What is the difference between copy() and deepcopy()?](https://discourse.julialang.org/t/what-is-the-difference-between-copy-and-deepcopy/3918)

<div class="topic-metadata">

**Author:** [@leejm516](https://discourse.julialang.org/u/leejm516)\
**Replies:** 7\
**Last updated:** [January 1, 2024, 5:39am UTC](https://discourse.julialang.org/t/what-is-the-difference-between-copy-and-deepcopy/3918 "2024-01-01T05:39:44Z")

</div>

julia\> a = \[1,2,3,4\]; julia\> b = a; julia\> a\[1\] = 4; julia\> b 4-element Array{Int64,1}: 4 2 3 4 This is the shallow copy AFAIK. But at a glance, I couldn’t find clear differences between copy() and deepcopy(), alth…

---

## [Possible performance improvements for \`deepcopy\`?](https://discourse.julialang.org/t/possible-performance-improvements-for-deepcopy/107719)

<div class="topic-metadata">

**Author:** [@Tortar](https://discourse.julialang.org/u/Tortar)\
**Replies:** 4\
**Last updated:** [December 21, 2023, 1:09am UTC](https://discourse.julialang.org/t/possible-performance-improvements-for-deepcopy/107719 "2023-12-21T01:09:42Z")

</div>

I created this small package here GitHub - Tortar/FastDeepCopy.jl which outperform the current deepcopy implementation by quite a bit, for a vector of mutable structs it is more than 200 times faster as you can see in th…

---

## [Copying mutable structs without using deepcopy()](https://discourse.julialang.org/t/copying-mutable-structs-without-using-deepcopy/106648)

<div class="topic-metadata">

**Author:** [@rolim.gustavo](https://discourse.julialang.org/u/rolim.gustavo)\
**Replies:** 4\
**Last updated:** [November 24, 2023, 8:37am UTC](https://discourse.julialang.org/t/copying-mutable-structs-without-using-deepcopy/106648 "2023-11-24T08:37:30Z")

</div>

Dear colleagues, I am working on an optimization problem where I have to group a permutation of jobs, which have some characteristics. A solution to this problem can be easily represented as a vector of vectors of a typ…

---

## [Deep copy of Array in Struct in NamedTuple](https://discourse.julialang.org/t/deep-copy-of-array-in-struct-in-namedtuple/102349)

<div class="topic-metadata">

**Author:** [@DanDoe](https://discourse.julialang.org/u/DanDoe)\
**Replies:** 3\
**Last updated:** [August 1, 2023, 12:17pm UTC](https://discourse.julialang.org/t/deep-copy-of-array-in-struct-in-namedtuple/102349 "2023-08-01T12:17:47Z")

</div>

I am creating and returning a NamedTuple cache mystruct = MyStruct{Float64}(42, 42, 42) cache = (; foo, bar, mystruct) return cache where mystruct is an object of the mutable struct MyStruct which contains an array mu…

---

## [Copy. vs. deepcopy vs. .=](https://discourse.julialang.org/t/copy-vs-deepcopy-vs/95097)

<div class="topic-metadata">

**Author:** [@prittjam](https://discourse.julialang.org/u/prittjam)\
**Replies:** 6\
**Last updated:** [February 23, 2023, 9:56pm UTC](https://discourse.julialang.org/t/copy-vs-deepcopy-vs/95097 "2023-02-23T21:56:59Z")

</div>

Despite many threads on this, I’m still confused. I want to avoid allocation but something is not working in my code. My setup is like the one below. Can someone explain the difference in this context, A = (rand(3),ran…

---

## [Duplicating integrator with KLUFactorization(; reuse\_symbolic = true)](https://discourse.julialang.org/t/duplicating-integrator-with-klufactorization-reuse-symbolic-true/91635)

<div class="topic-metadata">

**Author:** [@DanielVandH](https://discourse.julialang.org/u/DanielVandH)\
**Replies:** 4\
**Last updated:** [December 14, 2022, 12:38pm UTC](https://discourse.julialang.org/t/duplicating-integrator-with-klufactorization-reuse-symbolic-true/91635 "2022-12-14T12:38:11Z")

</div>

I have a problem that uses multithreading and I’ve set it up to copy some integrators defining a differential equation problem. I’m running into issues with KLUFactorization(), namely my method fails when KLUFactorizatio…

---

## [Misunderstanding of the copy() function](https://discourse.julialang.org/t/misunderstanding-of-the-copy-function/90078)

<div class="topic-metadata">

**Author:** [@Zackyzz](https://discourse.julialang.org/u/Zackyzz)\
**Replies:** 1\
**Last updated:** [November 10, 2022, 10:37pm UTC](https://discourse.julialang.org/t/misunderstanding-of-the-copy-function/90078 "2022-11-10T22:37:26Z")

</div>

Hello! I come from a Lisp background and mostly when I change a parameter inside a function I prefer to have it modified only in that function. Now from what I’ve seen from the bellow code, lst actually gets modified to…

---

## [Small changes on large arrays of custom types: mutable or immutable? shallow or deepcopy?](https://discourse.julialang.org/t/small-changes-on-large-arrays-of-custom-types-mutable-or-immutable-shallow-or-deepcopy/82335)

<div class="topic-metadata">

**Author:** [@skraemer](https://discourse.julialang.org/u/skraemer)\
**Replies:** 3\
**Last updated:** [June 7, 2022, 12:56pm UTC](https://discourse.julialang.org/t/small-changes-on-large-arrays-of-custom-types-mutable-or-immutable-shallow-or-deepcopy/82335 "2022-06-07T12:56:51Z")

</div>

(I have previously asked this on the New to Julia category, but I think might rather belong here) There are a few things somehow all related about the following situation concerning mutability to which I can not find a …

---

## [What is the difference between copy and deepcopy?](https://discourse.julialang.org/t/what-is-the-difference-between-copy-and-deepcopy/82149)

<div class="topic-metadata">

**Author:** [@Raymond](https://discourse.julialang.org/u/Raymond)\
**Replies:** 21\
**Last updated:** [June 4, 2022, 3:36am UTC](https://discourse.julialang.org/t/what-is-the-difference-between-copy-and-deepcopy/82149 "2022-06-04T03:36:20Z")

</div>

Dear all, I want to know the difference between copy and deepcopy? Many thanks.

---

## [Add deepcopy operator := to avoid bugs](https://discourse.julialang.org/t/add-deepcopy-operator-to-avoid-bugs/70793)

<div class="topic-metadata">

**Author:** [@martin.demare](https://discourse.julialang.org/u/martin.demare)\
**Replies:** 7\
**Last updated:** [November 2, 2021, 11:41am UTC](https://discourse.julialang.org/t/add-deepcopy-operator-to-avoid-bugs/70793 "2021-11-02T11:41:20Z")

</div>

When doing everyday data analysis, avoiding bugs is often more time saving than having great code performance. One of my reoccurring bugs is that I forget that z=x doesn’t create a copy of x if x is an array. Therefo…

---

## [Why is \`copy(adjoint)\` not an adjoint object?](https://discourse.julialang.org/t/why-is-copy-adjoint-not-an-adjoint-object/55371)

<div class="topic-metadata">

**Author:** [@Krastanov](https://discourse.julialang.org/u/Krastanov)\
**Replies:** 12\
**Last updated:** [February 18, 2021, 7:03am UTC](https://discourse.julialang.org/t/why-is-copy-adjoint-not-an-adjoint-object/55371 "2021-02-18T07:03:11Z")

</div>

This surprised me: julia\> typeof(copy(rand(4,4)')) Matrix{Float64} (alias for Array{Float64, 2}) julia\> typeof(deepcopy(rand(4,4)')) LinearAlgebra.Adjoint{Float64, Matrix{Float64}} I have an algorithm which looks pret…

---

## [Why is deepcopy() -ing Sets slower than Arrays of the same size?](https://discourse.julialang.org/t/why-is-deepcopy-ing-sets-slower-than-arrays-of-the-same-size/53020)

<div class="topic-metadata">

**Author:** [@bsuwal](https://discourse.julialang.org/u/bsuwal)\
**Replies:** 4\
**Last updated:** [January 8, 2021, 3:23am UTC](https://discourse.julialang.org/t/why-is-deepcopy-ing-sets-slower-than-arrays-of-the-same-size/53020 "2021-01-08T03:23:39Z")

</div>

I have the following code: function deepcopy\_n\_times(obj::Set{Int}, n::Int) for i in 1:n deepcopy(obj) end end function deepcopy\_n\_times(obj::Array{Int, 1}, n::Int) for i in 1:n deepcopy(obj…

---

## [JuMPv0.21 and deepcopy](https://discourse.julialang.org/t/jumpv0-21-and-deepcopy/36051)

<div class="topic-metadata">

**Author:** [@dmetivie](https://discourse.julialang.org/u/dmetivie)\
**Replies:** 7\
**Last updated:** [March 17, 2020, 5:03pm UTC](https://discourse.julialang.org/t/jumpv0-21-and-deepcopy/36051 "2020-03-17T17:03:17Z")

</div>

Hi, In JuMP Version 0.21 it says Calling deepcopy(::AbstractModel) now throws an error. Is there a new way to do deepcopy()? As far as I understand copy() is not what I want because the copied and original model are…

---

## [Difference between deepcopy() and deepcopy.() ? Do I have to broadcast deepcopy over lists?](https://discourse.julialang.org/t/difference-between-deepcopy-and-deepcopy-do-i-have-to-broadcast-deepcopy-over-lists/34460)

<div class="topic-metadata">

**Author:** [@Volker](https://discourse.julialang.org/u/Volker)\
**Replies:** 3\
**Last updated:** [February 11, 2020, 10:55am UTC](https://discourse.julialang.org/t/difference-between-deepcopy-and-deepcopy-do-i-have-to-broadcast-deepcopy-over-lists/34460 "2020-02-11T10:55:49Z")

</div>

I have seen a behaviour that confused me a little bit. I have a list with structs (list\_of\_structs) and I selected specific indices of it with an integer array (idx\_array). When I then manipulate structs of the list\_of\_s…

---

## [Push! seems to be incorrect in this situation](https://discourse.julialang.org/t/push-seems-to-be-incorrect-in-this-situation/31589)

<div class="topic-metadata">

**Author:** [@ke\_xu](https://discourse.julialang.org/u/ke_xu)\
**Replies:** 7\
**Last updated:** [November 27, 2019, 7:56pm UTC](https://discourse.julialang.org/t/push-seems-to-be-incorrect-in-this-situation/31589 "2019-11-27T19:56:43Z")

</div>

function Append(vectorOfVector, vector) vector\[1\] = 0 push!(vectorOfVector,vector) vector\[1\] = 1 push!(vectorOfVector,vector) end vectorOfVectors = Vector{Vector{Int64}}() vector = Vector{Int64}(undef,1)…

---

## [Deepcopying struct with array fields](https://discourse.julialang.org/t/deepcopying-struct-with-array-fields/27430)

<div class="topic-metadata">

**Author:** [@DocDave](https://discourse.julialang.org/u/DocDave)\
**Replies:** 14\
**Last updated:** [August 12, 2019, 3:16pm UTC](https://discourse.julialang.org/t/deepcopying-struct-with-array-fields/27430 "2019-08-12T15:16:53Z")

</div>

I have a struct containing fields, some of which are multidimensional Arrays, however I want to be able to copy (both deep and shallow) between instances of this type. Comments on an answer from SO indicate that this can…
