support multi-arg push! and unshift!#4782
Conversation
|
(On a separate note, I feel like there should be fallback versions of |
|
I like it. I'm curious about the overhead of splatting. Would it be any slower if we removed |
|
At the very least, splatting has to make an extra copy of the data to convert it to a tuple, so that can't be good if you have a large array... |
|
I think that |
|
Just to be clear, I'm not proposing eliminating prepend and append. I think they have to stay for performance and memory reasons, not to mention convenience. That's not what this patch is about. |
|
@stevengj, I don't think there was any misunderstanding. More related to this request, this behavior would conflict with @StefanKarpinski's suggestion in #3439 (comment), regarding the potential use of |
|
I'm fine with this change, but I think it would be better for implementers of |
|
@JeffBezanson, I agree, but I figured that kind of optimization could be done later; this version is no worse than what we have now. |
support multi-arg push! and unshift!
This patch extends
push!andunshift!to accept multiple arguments:push!(A, items...)andunshift!(A, items...), whenever the 1-item versions are defined. For example:Note that the ordering of the arguments is the same as their ordering in the resulting array (so the items are pushed from left to right, but unshifted from right to left). This seems sensible to me (changing the ordering is a bit surprising), it mirrors
append!andprepend!, and e.g. the Javascript push/unshift functions have the same semantics.By default, these are implemented in terms of the 1-item versions. Specific types could, of course, define specialized multi-argument versions, e.g.
push!(A::Array, items...)could be defined to grow the array by several items at once and avoid overhead. But this is an optimization that can be done later if it seems important. The main thing is that this patch does not introduce any performance regressions, it just increases convenience.