- 
          
- 
                Notifications
    You must be signed in to change notification settings 
- Fork 5.7k
Closed
Description
The behaviour for broadcasted Period subtraction is currently
julia> Day(1) - [Day(2), Day(3)]
2-element Array{Base.Dates.Day,1}:
 1 day 
 2 daysand
julia> Day(1) .- [Day(2), Day(3)]
2-element Array{Base.Dates.Day,1}:
 1 day 
 2 dayswhereas presumably the desired output is:
julia> [Day(1) - Day(2), Day(1) - Day(3)]
2-element Array{Base.Dates.Day,1}:
 -1 day 
 -2 daysLooking at dates/periods.jl, the first two method definitions here work fine for + but not -:
for op in (:.+, :.-)
op_ = symbol(string(op)[2:end])
    @eval begin
        ($op){P<:Period}(x::P,Y::StridedArray{P}) = ($op)(Y,x)
        ($op_){P<:Period}(x::P,Y::StridedArray{P}) = ($op)(Y,x)
        ($op_){P<:Period}(Y::StridedArray{P},x::P) = ($op)(Y,x)
    end
endOne option might be to implement unary addition (as identity) and then do
for op in (:.+, :.-)
op_ = symbol(string(op)[2:end])
    @eval begin
        ($op){P<:Period}(x::P,Y::StridedArray{P}) = ($op_)(($op)(Y,x))
        ($op_){P<:Period}(x::P,Y::StridedArray{P}) = ($op_)(($op)(Y,x))
        ($op_){P<:Period}(Y::StridedArray{P},x::P) = ($op)(Y,x)
    end
endor, just hardcode the definitions as
(.+){P<:Period}(x::P,Y::StridedArray{P}) = Y .+ x
(+){P<:Period}(x::P,Y::StridedArray{P}) = Y .+ x
(+){P<:Period}(Y::StridedArray{P},x::P) = Y .+ x
(.-){P<:Period}(x::P,Y::StridedArray{P}) = (-Y) .+ x
(-){P<:Period}(x::P,Y::StridedArray{P}) = (-Y) .+ x
(-){P<:Period}(Y::StridedArray{P},x::P) = Y .- xwhich would actually be fewer LOC... maybe more readable too.
Metadata
Metadata
Assignees
Labels
No labels