Method of Base.:+ defined for times but not ints?

Just an observation.

This isn’t defined

[1] + 1
# ERROR: MethodError: no method matching +(::Vector{Int64}, ::Int64)

But this is

[DateTime(2023,1,1)] + Second(1)
# 1-element Vector{DateTime}:
#  2023-01-01T00:00:01

I don’t have an opinion on what the behavior should be, It’s just surprising to see different behaviors.

function (+)(x::AbstractArray{<:TimeType}, y::GeneralPeriod)
    # depwarn("non-broadcasted arithmetic is deprecated for Dates.TimeType; use broadcasting instead", nothing)
    x .+ y
end

Looks like the deprecation is commented out.

1 Like

Sounds like I shouldn’t use it then. Thank you.

Care to tell me how you found this?
I know of methods(Base.:+), but that displays like 200 methods.

julia> @which [DateTime(2023,1,1)] + Second(1)
+(x::AbstractArray{<:TimeType}, y::Union{Dates.CompoundPeriod, Period})
     @ Dates ~/julia-1.9.0-rc2/share/julia/stdlib/v1.9/Dates/src/deprecated.jl:6                                                        
julia> @less [DateTime(2023,1,1)] + Second(1)

Just to make sure, the right way to do this is via broadcasting or maybe map.

julia> [DateTime(2023,1,1)] .+ Second(1)                            1-element Vector{DateTime}:        
2023-01-01T00:00:01
                                  
julia> [1] .+ 1                   
1-element Vector{Int64}:           
2
5 Likes