From e7dbf266ff3ea09c9921b3ea5adcc8f8f4e1bfcb Mon Sep 17 00:00:00 2001 From: felix Date: Fri, 10 Feb 2017 16:58:46 +0000 Subject: [PATCH 1/2] extend mr_empty when the type is supplied We allow users to call `sum(T::Type, xs)`. Currently, e.g. `sum(Float64, [])` gives an error because the `eltype` of `[]` is `Any` and so inference fails. But the fact that the user supplied a type means that we know the intended `eltype`, so we can use it to set e.g. `sum(Float64, []) = zero(Float64)`. --- base/reduce.jl | 3 +++ 1 file changed, 3 insertions(+) diff --git a/base/reduce.jl b/base/reduce.jl index 2b5602553d285..622116840e5c0 100644 --- a/base/reduce.jl +++ b/base/reduce.jl @@ -230,6 +230,9 @@ pairwise_blocksize(::typeof(abs2), ::typeof(+)) = 4096 _empty_reduce_error() = throw(ArgumentError("reducing over an empty collection is not allowed")) mr_empty(f, op, T) = _empty_reduce_error() # use zero(T)::T to improve type information when zero(T) is not defined +# f::Callable = Union{Type, Function}; when f::Type, we can do better +mr_empty(S::Type, op::typeof(+), T) = r_promote(op, zero(S)::S) +mr_empty(s::Type, op::typeof(*), T) = r_promote(op, one(S)::S) mr_empty(::typeof(identity), op::typeof(+), T) = r_promote(op, zero(T)::T) mr_empty(::typeof(abs), op::typeof(+), T) = r_promote(op, abs(zero(T)::T)) mr_empty(::typeof(abs2), op::typeof(+), T) = r_promote(op, abs2(zero(T)::T)) From 078586e5ee4148d20dad4e45c64b38a85522a158 Mon Sep 17 00:00:00 2001 From: felix Date: Fri, 10 Feb 2017 17:09:21 +0000 Subject: [PATCH 2/2] typo --- base/reduce.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/reduce.jl b/base/reduce.jl index 622116840e5c0..cae8b6fd906c7 100644 --- a/base/reduce.jl +++ b/base/reduce.jl @@ -232,7 +232,7 @@ mr_empty(f, op, T) = _empty_reduce_error() # use zero(T)::T to improve type information when zero(T) is not defined # f::Callable = Union{Type, Function}; when f::Type, we can do better mr_empty(S::Type, op::typeof(+), T) = r_promote(op, zero(S)::S) -mr_empty(s::Type, op::typeof(*), T) = r_promote(op, one(S)::S) +mr_empty(S::Type, op::typeof(*), T) = r_promote(op, one(S)::S) mr_empty(::typeof(identity), op::typeof(+), T) = r_promote(op, zero(T)::T) mr_empty(::typeof(abs), op::typeof(+), T) = r_promote(op, abs(zero(T)::T)) mr_empty(::typeof(abs2), op::typeof(+), T) = r_promote(op, abs2(zero(T)::T))