# A simple inference bug with Dates.Period?

**URL:** <https://discourse.julialang.org/t/a-simple-inference-bug-with-dates-period/79966>\
**Category:** General Usage\
**Tags:** inference, dates, potential-bug\
**Created:** [April 25, 2022, 1:35am UTC](https://discourse.julialang.org/t/a-simple-inference-bug-with-dates-period/79966 "2022-04-25T01:35:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![braydenware](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/braydenware/32/2612_2.png) [@braydenware](https://discourse.julialang.org/u/braydenware)\
**Post date:** [April 25, 2022, 1:35am UTC](https://discourse.julialang.org/t/a-simple-inference-bug-with-dates-period/79966/1 "2022-04-25T01:35:31Z")

</div>

In some simple arithmetic subtracting types with `Dates.CompoundPeriod`, a bug arises when you try to subtract a `Dates.CompoundPeriod` that is equal to `0`. However, it does not seem to be a problem with the `Dates` code but rather an inference bug that occurs when broadcasting a Base.- over an empty vector of type Dates.Period.

```julia
julia> using Dates

julia> -Int[]
Int64[]

julia> -Dates.Period[]
Any[]

julia> t1 = Dates.Day(0)+Dates.Hour(0)
empty period

julia> t1.periods
Period[]

julia> -t1
ERROR: MethodError: no method matching Dates.CompoundPeriod(::Vector{Any})
Closest candidates are:
  Dates.CompoundPeriod(::Vector{Period}) at /Applications/Julia-1.7x86.app/Contents/Resources/julia/share/julia/stdlib/v1.7/Dates/src/periods.jl:167
  Dates.CompoundPeriod(::Vector{<:Period}) at /Applications/Julia-1.7x86.app/Contents/Resources/julia/share/julia/stdlib/v1.7/Dates/src/periods.jl:230
  Dates.CompoundPeriod(::Time) at /Applications/Julia-1.7x86.app/Contents/Resources/julia/share/julia/stdlib/v1.7/Dates/src/periods.jl:232
  ...
Stacktrace:
 [1] -(x::Dates.CompoundPeriod)
   @ Dates /Applications/Julia-1.7x86.app/Contents/Resources/julia/share/julia/stdlib/v1.7/Dates/src/periods.jl:368
 [2] top-level scope
   @ REPL[172]:1

```

---

<div class="post-metadata">

**Author:** ![braydenware](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/braydenware/32/2612_2.png) [@braydenware](https://discourse.julialang.org/u/braydenware)\
**Post date:** [April 25, 2022, 1:54am UTC](https://discourse.julialang.org/t/a-simple-inference-bug-with-dates-period/79966/2 "2022-04-25T01:54:48Z")

</div>

On second thought, perhaps this is the intended broadcast behavior. In this case, the output type is again not as strict as possible:

```julia
julia> T = Union{Int, Float64}
Union{Float64, Int64}

julia> v = T[]
Union{Float64, Int64}[]

julia> -v
Real[]

```

In either case, the arithmetic issues in `Dates` could be easily fixed by special casing the empty vector in the `Dates` code:

```julia
(-)(x::CompoundPeriod) = CompoundPeriod(-x.periods)

```

could be replaced by

```julia
function (-)(x::CompoundPeriod)
    if isempty(x.periods)
        return CompoundPeriod(Dates.Period[])
    end
    return CompoundPeriod(-x.periods)
end

```

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [April 25, 2022, 3:36am UTC](https://discourse.julialang.org/t/a-simple-inference-bug-with-dates-period/79966/3 "2022-04-25T03:36:02Z")

</div>

You are on to something.  
Currently `empty - notempty` works but `notempty - empty` errors, that is a bug which your suggestion fixes (its probably ok to return `x` itself if empty).  
I have run into the lack of iteration over CompoundPeriods, which is easy enough, and handles the empty case:

```julia
Base.iterate(x::Dates.CompoundPeriod) = 
    iterate(x.periods, 1)
Base.iterate(x::Dates.CompoundPeriod, state) =
    iterate(x.periods, state)

```
