# Is there such a thing like a pushcopy!(...)?

**URL:** <https://discourse.julialang.org/t/is-there-such-a-thing-like-a-pushcopy/84930>\
**Category:** Performance\
**Created:** [July 28, 2022, 4:53pm UTC](https://discourse.julialang.org/t/is-there-such-a-thing-like-a-pushcopy/84930 "2022-07-28T16:53:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![fipelle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fipelle/32/4772_2.png) [@fipelle](https://discourse.julialang.org/u/fipelle)\
**Post date:** [July 28, 2022, 4:53pm UTC](https://discourse.julialang.org/t/is-there-such-a-thing-like-a-pushcopy/84930/1 "2022-07-28T16:53:08Z")

</div>

Hi,

I was wondering if there’s an option or a function in base that pushes a copy rather than a reference into a collection, similarly to what

```julia
pushcopy!(collection::AbstractVector, item::AbstractArray) = push!(collection, copy(item));

```

would do. If not, are there any plans to add it?

---

<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:** [July 28, 2022, 5:04pm UTC](https://discourse.julialang.org/t/is-there-such-a-thing-like-a-pushcopy/84930/2 "2022-07-28T17:04:57Z")

</div>

I don’t know about any, no, and I don’t think it’s planned. People generally try to not copy things around - that copy will always happen and can’t be elided, since it has to create a distinct object.

---

<div class="post-metadata">

**Author:** ![fipelle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fipelle/32/4772_2.png) [@fipelle](https://discourse.julialang.org/u/fipelle)\
**Post date:** [July 28, 2022, 5:08pm UTC](https://discourse.julialang.org/t/is-there-such-a-thing-like-a-pushcopy/84930/3 "2022-07-28T17:08:59Z")

</div>

I don’t think it does with `push!` - see [Using push!](https://discourse.julialang.org/t/using-push/30935) for example.

---

<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:** [July 28, 2022, 5:34pm UTC](https://discourse.julialang.org/t/is-there-such-a-thing-like-a-pushcopy/84930/4 "2022-07-28T17:34:53Z")

</div>

I was referring to `copy` - that can’t ever be elided. In e.g. Rust, with copy semantics and explicit references, their copies can often be elided. We can’t do that in julia as far as I know, if there’s a `copy` we have to create a new object.

Maybe I’m misunderstanding what you need this for though.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [July 28, 2022, 5:51pm UTC](https://discourse.julialang.org/t/is-there-such-a-thing-like-a-pushcopy/84930/5 "2022-07-28T17:51:43Z")

</div>

```julia
push!(collection, copy(item))

```

seems much nicer to me than

```julia
pushcopy!(collection, item)

```

The former composes functionality in a very natural and obvious way, so what is the gain of introducing an extra function instead?

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [July 28, 2022, 6:41pm UTC](https://discourse.julialang.org/t/is-there-such-a-thing-like-a-pushcopy/84930/6 "2022-07-28T18:41:07Z")

</div>

I agree with @DNF. The current way seems like it is preferable, unless there is some obvious performance left on the table that could be corrected by adding a `pushcopy!` method which does the copy internally. I don’t think this is the case however.
