Skip to content

Commit

Permalink
Throw ArgumentError not plain ErrorException
Browse files Browse the repository at this point in the history
  • Loading branch information
nickrobinson251 committed Aug 31, 2019
1 parent 0305622 commit 04d1c74
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
20 changes: 11 additions & 9 deletions base/iterators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1097,30 +1097,32 @@ length(s::Stateful) = length(s.itr) - s.taken
"""
only(x)
Returns the one and only element of collection `x`, and throws an error if the collection
has zero or multiple elements.
Returns the one and only element of collection `x`, and throws an `ArgumentError` if the
collection has zero or multiple elements.
"""
Base.@propagate_inbounds function only(x)
i = iterate(x)
@boundscheck if i === nothing
error("Collection is empty, must contain exactly 1 element")
throw(ArgumentError("Collection is empty, must contain exactly 1 element"))
end
(ret, state) = i
j = iterate(x, state)
@boundscheck if j !== nothing
error("Collection has multiple elements, must contain exactly 1 element")
throw(ArgumentError("Collection has multiple elements, must contain exactly 1 element"))
end
return ret
end

# Collections of known size
only(x::Tuple{}) = error("Tuple is empty, must contain exactly 1 element")
only(x::Tuple{}) = throw(ArgumentError("Tuple is empty, must contain exactly 1 element")
only(x::Tuple{Any}) = x[1]
only(x::Tuple) = error("Tuple contains $(length(x)) elements, must contain exactly 1 element")

only(x::Tuple) = throw(
ArgumentError("Tuple contains $(length(x)) elements, must contain exactly 1 element")
)
only(a::AbstractArray{<:Any, 0}) = @inbounds return a[]

only(x::NamedTuple{<:Any, <:Tuple{Any}}) = first(x)
only(x::NamedTuple) = error("NamedTuple contains $(length(x)) elements, must contain exactly 1 element")
only(x::NamedTuple) = throw(
ArgumentError("NamedTuple contains $(length(x)) elements, must contain exactly 1 element")
)

end
22 changes: 10 additions & 12 deletions test/iterators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,6 @@ end
@test Base.IteratorEltype(repeated(0, 5)) == Base.HasEltype()
@test Base.IteratorSize(zip(repeated(0), repeated(0))) == Base.IsInfinite()


# product
# -------

Expand Down Expand Up @@ -411,7 +410,6 @@ for n in [5,6]
[(1,1),(2,2),(3,3),(4,4),(5,5)]
end


@test join(map(x->string(x...), partition("Hello World!", 5)), "|") ==
"Hello| Worl|d!"

Expand Down Expand Up @@ -650,22 +648,22 @@ end

@testset "only" begin
@test only([3]) === 3
@test_throws ErrorException only([])
@test_throws ErrorException only([3, 2])
@test_throws ArgumentError only([])
@test_throws ArgumentError only([3, 2])

@test @inferred(only((3,))) === 3
@test_throws ErrorException only(())
@test_throws ErrorException only((3, 2))
@test_throws ArgumentError only(())
@test_throws ArgumentError only((3, 2))

@test only(Dict(1=>3)) === (1=>3)
@test_throws ErrorException only(Dict{Int,Int}())
@test_throws ErrorException only(Dict(1=>3, 2=>2))
@test_throws ArgumentError only(Dict{Int,Int}())
@test_throws ArgumentError only(Dict(1=>3, 2=>2))

@test only(Set([3])) === 3
@test_throws ErrorException only(Set(Int[]))
@test_throws ErrorException only(Set([3,2]))
@test_throws ArgumentError only(Set(Int[]))
@test_throws ArgumentError only(Set([3,2]))

@test @inferred(only((;a=1))) === 1
@test_throws ErrorException only(NamedTuple())
@test_throws ErrorException only((a=3, b=2.0))
@test_throws ArgumentError only(NamedTuple())
@test_throws ArgumentError only((a=3, b=2.0))
end

0 comments on commit 04d1c74

Please sign in to comment.