From 01fd0a1fdce0b077e5c367e1ca5637a2b2a4ee97 Mon Sep 17 00:00:00 2001 From: Tim Holy Date: Wed, 29 Jul 2015 18:41:27 -0500 Subject: [PATCH] Base AbstractArray{T<:Date} operations on promote_op This avoids deprecation warnings with packages like Images and DataArrays (hopefully others too). The key point seems to be to avoid specializing on the element type while being generic about the container. --- base/dates/arithmetic.jl | 36 ++++++++++++++++++++++++++---------- base/functors.jl | 3 +++ 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/base/dates/arithmetic.jl b/base/dates/arithmetic.jl index f81780ac6340c..3891a06a4b88e 100644 --- a/base/dates/arithmetic.jl +++ b/base/dates/arithmetic.jl @@ -82,12 +82,6 @@ for op in (:.+, :.-) ($op){P<:GeneralPeriod}(y::TimeType, x::StridedArray{P}) = ($op)(x,y) ($op_){T<:TimeType,P<:GeneralPeriod}(x::StridedArray{P}, y::T) = ($op)(x,y) ($op_){P<:GeneralPeriod}(y::TimeType, x::StridedArray{P}) = ($op)(x,y) - - # AbstractArray{TimeType}, StridedArray{GeneralPeriod} - ($op_){T<:TimeType,P<:GeneralPeriod}(x::Range{T}, y::StridedArray{P}) = ($op_)(collect(x),y) - ($op_){T<:TimeType,P<:GeneralPeriod}(x::AbstractArray{T}, y::StridedArray{P}) = - reshape(TimeType[($op_)(x[i],y[i]) for i in eachindex(x, y)], promote_shape(size(x),size(y))) - ($op_){T<:TimeType,P<:GeneralPeriod}(y::StridedArray{P}, x::AbstractArray{T}) = ($op_)(x,y) end end @@ -100,7 +94,29 @@ end # AbstractArray{TimeType}, AbstractArray{TimeType} (-){T<:TimeType}(x::OrdinalRange{T}, y::OrdinalRange{T}) = collect(x) - collect(y) (-){T<:TimeType}(x::Range{T}, y::Range{T}) = collect(x) - collect(y) -(-){T<:TimeType}(x::AbstractArray{T}, y::Range{T}) = y - collect(x) -(-){T<:TimeType}(x::Range{T}, y::AbstractArray{T}) = collect(x) - y -(-){T<:TimeType}(x::AbstractArray{T}, y::AbstractArray{T}) = - reshape(Period[x[i] - y[i] for i in eachindex(x, y)], promote_shape(size(x),size(y))) + +# promotion rules + +for (op,F) in ((:+, Base.AddFun), + (:-, Base.SubFun), + (:.+, Base.DotAddFun), + (:.-, Base.DotSubFun)) + @eval begin + Base.promote_op{P<:Period}(::$F, ::Type{P}, ::Type{P}) = P + Base.promote_op{P1<:Period,P2<:Period}(::$F, ::Type{P1}, ::Type{P2}) = CompoundPeriod + Base.promote_op{D<:Date}(::$F, ::Type{D}, ::Type{D}) = Day + Base.promote_op{D<:DateTime}(::$F, ::Type{D}, ::Type{D}) = Millisecond + end +end + +for (op,F) in ((:/, Base.RDivFun), + (:%, Base.RemFun), + (:div, Base.IDivFun), + (:mod, Base.ModFun), + (:./, Base.DotRDivFun), + (:.%, Base.DotRemFun)) + @eval begin + Base.promote_op{P<:Period}(::$F, ::Type{P}, ::Type{P}) = typeof($op(1,1)) + Base.promote_op{P<:Period,R<:Real}(::$F, ::Type{P}, ::Type{R}) = P + end +end diff --git a/base/functors.jl b/base/functors.jl index 18df3e5e7d19c..d1a633c08686a 100644 --- a/base/functors.jl +++ b/base/functors.jl @@ -58,6 +58,9 @@ call(::DotMulFun, x, y) = x .* y immutable RDivFun <: Func{2} end call(::RDivFun, x, y) = x / y +immutable DotRDivFun <: Func{2} end +call(::DotRDivFun, x, y) = x ./ y + immutable LDivFun <: Func{2} end call(::LDivFun, x, y) = x \ y