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

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

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

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

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.

I don’t think it does with push! - see Using push! for example.

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.

push!(collection, copy(item))

seems much nicer to me than

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?

8 Likes

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.