Skip to content

Commit

Permalink
combine reduce_empty methods for Union{} eltypes
Browse files Browse the repository at this point in the history
With #49470, these can all be dispatched to the same method now,
avoiding unnecessary code duplication for this case.
  • Loading branch information
vtjnash committed Oct 30, 2023
1 parent 96147bb commit 6d11fd3
Showing 1 changed file with 2 additions and 5 deletions.
7 changes: 2 additions & 5 deletions base/reduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,8 @@ _empty_reduce_error() = throw(ArgumentError("reducing over an empty collection i
_empty_reduce_error(@nospecialize(f), @nospecialize(T::Type)) = throw(ArgumentError("""
reducing with $f over an empty collection of element type $T is not allowed.
You may be able to prevent this error by supplying an `init` value to the reducer."""))
reduce_empty(f, ::Type{Union{}}, splat...) = _empty_reduce_error(f, Union{})


"""
Base.reduce_empty(op, T)
Expand All @@ -339,20 +341,15 @@ is generally ambiguous, and especially so when the element type is unknown).
As an alternative, consider supplying an `init` value to the reducer.
"""
reduce_empty(::typeof(+), ::Type{Union{}}) = _empty_reduce_error(+, Union{})
reduce_empty(::typeof(+), ::Type{T}) where {T} = zero(T)
reduce_empty(::typeof(+), ::Type{Bool}) = zero(Int)
reduce_empty(::typeof(*), ::Type{Union{}}) = _empty_reduce_error(*, Union{})
reduce_empty(::typeof(*), ::Type{T}) where {T} = one(T)
reduce_empty(::typeof(*), ::Type{<:AbstractChar}) = ""
reduce_empty(::typeof(&), ::Type{Bool}) = true
reduce_empty(::typeof(|), ::Type{Bool}) = false

reduce_empty(::typeof(add_sum), ::Type{Union{}}) = _empty_reduce_error(add_sum, Union{})
reduce_empty(::typeof(add_sum), ::Type{T}) where {T} = reduce_empty(+, T)
reduce_empty(::typeof(add_sum), ::Type{T}) where {T<:SmallSigned} = zero(Int)
reduce_empty(::typeof(add_sum), ::Type{T}) where {T<:SmallUnsigned} = zero(UInt)
reduce_empty(::typeof(mul_prod), ::Type{Union{}}) = _empty_reduce_error(mul_prod, Union{})
reduce_empty(::typeof(mul_prod), ::Type{T}) where {T} = reduce_empty(*, T)
reduce_empty(::typeof(mul_prod), ::Type{T}) where {T<:SmallSigned} = one(Int)
reduce_empty(::typeof(mul_prod), ::Type{T}) where {T<:SmallUnsigned} = one(UInt)
Expand Down

0 comments on commit 6d11fd3

Please sign in to comment.