Skip to content

Commit 6d11fd3

Browse files
committed
combine reduce_empty methods for Union{} eltypes
With #49470, these can all be dispatched to the same method now, avoiding unnecessary code duplication for this case.
1 parent 96147bb commit 6d11fd3

File tree

1 file changed

+2
-5
lines changed

1 file changed

+2
-5
lines changed

base/reduce.jl

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,8 @@ _empty_reduce_error() = throw(ArgumentError("reducing over an empty collection i
320320
_empty_reduce_error(@nospecialize(f), @nospecialize(T::Type)) = throw(ArgumentError("""
321321
reducing with $f over an empty collection of element type $T is not allowed.
322322
You may be able to prevent this error by supplying an `init` value to the reducer."""))
323+
reduce_empty(f, ::Type{Union{}}, splat...) = _empty_reduce_error(f, Union{})
324+
323325

324326
"""
325327
Base.reduce_empty(op, T)
@@ -339,20 +341,15 @@ is generally ambiguous, and especially so when the element type is unknown).
339341
340342
As an alternative, consider supplying an `init` value to the reducer.
341343
"""
342-
reduce_empty(::typeof(+), ::Type{Union{}}) = _empty_reduce_error(+, Union{})
343344
reduce_empty(::typeof(+), ::Type{T}) where {T} = zero(T)
344345
reduce_empty(::typeof(+), ::Type{Bool}) = zero(Int)
345-
reduce_empty(::typeof(*), ::Type{Union{}}) = _empty_reduce_error(*, Union{})
346346
reduce_empty(::typeof(*), ::Type{T}) where {T} = one(T)
347347
reduce_empty(::typeof(*), ::Type{<:AbstractChar}) = ""
348348
reduce_empty(::typeof(&), ::Type{Bool}) = true
349349
reduce_empty(::typeof(|), ::Type{Bool}) = false
350-
351-
reduce_empty(::typeof(add_sum), ::Type{Union{}}) = _empty_reduce_error(add_sum, Union{})
352350
reduce_empty(::typeof(add_sum), ::Type{T}) where {T} = reduce_empty(+, T)
353351
reduce_empty(::typeof(add_sum), ::Type{T}) where {T<:SmallSigned} = zero(Int)
354352
reduce_empty(::typeof(add_sum), ::Type{T}) where {T<:SmallUnsigned} = zero(UInt)
355-
reduce_empty(::typeof(mul_prod), ::Type{Union{}}) = _empty_reduce_error(mul_prod, Union{})
356353
reduce_empty(::typeof(mul_prod), ::Type{T}) where {T} = reduce_empty(*, T)
357354
reduce_empty(::typeof(mul_prod), ::Type{T}) where {T<:SmallSigned} = one(Int)
358355
reduce_empty(::typeof(mul_prod), ::Type{T}) where {T<:SmallUnsigned} = one(UInt)

0 commit comments

Comments
 (0)