Skip to content

Commit

Permalink
force push! and unshift! need at least one item
Browse files Browse the repository at this point in the history
  • Loading branch information
jakebolewski committed Mar 4, 2015
1 parent 31c12af commit ae5cb8d
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
2 changes: 0 additions & 2 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1453,10 +1453,8 @@ end

# multi-item push!, unshift! (built on top of type-specific 1-item version)
# (note: must not cause a dispatch loop when 1-item case is not defined)
push!(A) = A
push!(A, a, b) = push!(push!(A, a), b)
push!(A, a, b, c...) = push!(push!(A, a, b), c...)
unshift!(A) = A
unshift!(A, a, b) = unshift!(unshift!(A, b), a)
unshift!(A, a, b, c...) = unshift!(unshift!(A, c...), a, b)

Expand Down
4 changes: 2 additions & 2 deletions doc/stdlib/collections.rst
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,7 @@ Dequeues

.. function:: push!(collection, items...) -> collection

Insert zero or more ``items`` at the end of ``collection``.
Insert one or more ``items`` at the end of ``collection``.

.. doctest::

Expand Down Expand Up @@ -846,7 +846,7 @@ Dequeues

.. function:: unshift!(collection, items...) -> collection

Insert zero or more ``items`` at the beginning of ``collection``.
Insert one or more ``items`` at the beginning of ``collection``.

.. doctest::

Expand Down
6 changes: 6 additions & 0 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,12 @@ A = [NaN]; B = [NaN]
@test isequal(A,B)
@test A!==B

# test that single arugment push!, unshift! are not defined
let A = []
@test_throws MethodError push!(a)
@test_throws MethodError unshift!(a)
end

# complete testsuite for reducedim

# Inferred types
Expand Down

1 comment on commit ae5cb8d

@StefanKarpinski
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I support this primarily since if you're going to push! a splatted collection onto something, you should be using append! anyway, which handles the zero elements case correctly.

Please sign in to comment.