From 6d11fd3ebafa9e8bf552e4e6a863cf75f95a0d24 Mon Sep 17 00:00:00 2001 From: Jameson Nash Date: Mon, 30 Oct 2023 20:42:22 +0000 Subject: [PATCH] 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. --- base/reduce.jl | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/base/reduce.jl b/base/reduce.jl index d98b237e4997c1..b485d043af7731 100644 --- a/base/reduce.jl +++ b/base/reduce.jl @@ -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) @@ -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)