# Add deepcopy operator := to avoid bugs

**URL:** <https://discourse.julialang.org/t/add-deepcopy-operator-to-avoid-bugs/70793>\
**Category:** General Usage\
**Tags:** suggestions, syntax, deepcopy\
**Created:** [November 2, 2021, 10:09am UTC](https://discourse.julialang.org/t/add-deepcopy-operator-to-avoid-bugs/70793 "2021-11-02T10:09:44Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![martin.demare](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/martin.demare/32/25222_2.png) [@martin.demare](https://discourse.julialang.org/u/martin.demare)\
**Post date:** [November 2, 2021, 10:09am UTC](https://discourse.julialang.org/t/add-deepcopy-operator-to-avoid-bugs/70793/1 "2021-11-02T10:09:44Z")

</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.

**Therefore I suggest to add a new operator `:=` which assigns by creating a deepcopy.**

In the same spirit, a colon at the end of a function name could create deepcopies of the function arguments when passed to the function (such that in `my_fun:(x)` the input variable x can be manipulated without affecting the value outside the variable outside the function).

By using the above operator and function-syntax I agree to the resulting performance penalty. If the code needs to be optimised for speed at a later stage, it is not very hard to inspect the code, check that no variables are unintentionally changed, and remove the colon.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [November 2, 2021, 10:24am UTC](https://discourse.julialang.org/t/add-deepcopy-operator-to-avoid-bugs/70793/2 "2021-11-02T10:24:35Z")

</div>

> [@martin.demare](#):
>
> One of my reoccurring bugs is that I forget that ` z=x` doesn’t create a copy of `x` if `x` is an array.

An assignment _never_ copies, not for any type. It _always_ rebinds the name `z` to point to the same thing that `x` binds (i.e. refers) to. If you’re thinking of something like `z = x[1:3]` and how that copies - it’s the `x[1:3]` part (the _slice_) that copies, not the assignment.

Also, your suggestion about `:` would change julia quite a lot - note that we’re not necessarily at that stage of development anymore.

> [@PSA: Julia is not at that stage of development anymore](https://discourse.julialang.org/t/topic/44872/1):
>
> You heard about this brand new exciting programming language called Julia and have some suggestions for how it should work. Wonderful! We’re happy to have you here and help you learn the language and listen to your ideas! However, please be aware that “new” is a relative term. Yes, Julia is newer than, say, C, which was first created in 1972 and standardized in 1989. It is not, however, new in the sense that it is still being freely designed and changed in arbitrary ways. Julia development start…

---

<div class="post-metadata">

**Author:** ![dfdx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dfdx/32/120_2.png) [@dfdx](https://discourse.julialang.org/u/dfdx)\
**Post date:** [November 2, 2021, 10:28am UTC](https://discourse.julialang.org/t/add-deepcopy-operator-to-avoid-bugs/70793/3 "2021-11-02T10:28:09Z")

</div>

Did you consider creating a macro that modifies your code the way you need (e.g. rewriting `z = x` to `z = deepcopy(x)`)? Then you would be able to write something like this:

```julia
@copyall begin
    z = x # translated to z = deepcopy(x)
    u = my_fun(x) # translated to u = my_fun(deepcopy(x))
    ...

end

```

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [November 2, 2021, 10:32am UTC](https://discourse.julialang.org/t/add-deepcopy-operator-to-avoid-bugs/70793/4 "2021-11-02T10:32:30Z")

</div>

A macro like that doesn’t work well with functions that modify their arguments intentionally - and you may not know if that happens, if the function doesn’t follow the `!` convention. I suspect this will lead to more bugs than are prevented by deeply changing how julia feels & thinks about memory.

---

<div class="post-metadata">

**Author:** ![dfdx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dfdx/32/120_2.png) [@dfdx](https://discourse.julialang.org/u/dfdx)\
**Post date:** [November 2, 2021, 10:40am UTC](https://discourse.julialang.org/t/add-deepcopy-operator-to-avoid-bugs/70793/5 "2021-11-02T10:40:16Z")

</div>

> [@Sukera](#):
>
> A macro like that doesn’t work well with functions that modify their arguments intentionally - and you may not know if that happens, if the function doesn’t follow the `!` convention.

Isn’t it what the OP asked for?

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [November 2, 2021, 11:07am UTC](https://discourse.julialang.org/t/add-deepcopy-operator-to-avoid-bugs/70793/6 "2021-11-02T11:07:29Z")

</div>

> [@martin.demare](#):
>
> In the same spirit, a colon at the end of a function name could create deepcopies of the function arguments when passed to the function (such that in `my_fun:(x)` the input variable x can be manipulated without affecting the value outside the variable outside the function).

That is actually easy to implement:

```julia
(::Colon)(f::Function, args::Tuple) = f(map(deepcopy, args)...)
a = [1] 
b = [2]
copyto!:(a,b) # does not modify a

```

---

<div class="post-metadata">

**Author:** ![martin.demare](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/martin.demare/32/25222_2.png) [@martin.demare](https://discourse.julialang.org/u/martin.demare)\
**Post date:** [November 2, 2021, 11:29am UTC](https://discourse.julialang.org/t/add-deepcopy-operator-to-avoid-bugs/70793/7 "2021-11-02T11:29:33Z")

</div>

Hi Per,  
That bit of code impressed me a lot! 🙂 👍  
Thanks!!

---

<div class="post-metadata">

**Author:** ![martin.demare](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/martin.demare/32/25222_2.png) [@martin.demare](https://discourse.julialang.org/u/martin.demare)\
**Post date:** [November 2, 2021, 11:41am UTC](https://discourse.julialang.org/t/add-deepcopy-operator-to-avoid-bugs/70793/8 "2021-11-02T11:41:20Z")

</div>

Hi @dfdx  
Thanks for your input, I will look into the solution with the macro!  
PS. Per solved the function-call part of the original question.
