diff --git a/src/interval.jl b/src/interval.jl index bace476f..a95fd5fb 100644 --- a/src/interval.jl +++ b/src/interval.jl @@ -189,6 +189,14 @@ function Base.convert(::Type{T}, interval::Interval{T}) where T end end +function Base.convert(::Type{<:Interval{T}}, interval::Interval{S,L,R}) where {T,S,L,R} + return Interval{T,L,R}(first(interval), last(interval)) +end + +Base.promote_rule(::Type{Interval{T,L1,R1}}, ::Type{Interval{S,L2,R2}}) where {T,S,L1,R1,L2,R2} = Interval{promote_type(T,S), <:Union{L1,L2}, <:Union{R1,R2}} +# hacky way to fix situations where T and S are the same but L and R are different +Base.not_sametype(x::Tuple{Interval{T,L1,R1}, Interval{T,L2,R2}}, y::Tuple{Interval{T,L1,R1}, Interval{T,L2,R2}}) where {T,L1,R1,L2,R2} = nothing + ##### DISPLAY ##### function Base.show(io::IO, interval::Interval{T,L,R}) where {T,L,R} diff --git a/test/interval.jl b/test/interval.jl index bca310d5..472cc9f3 100644 --- a/test/interval.jl +++ b/test/interval.jl @@ -85,6 +85,7 @@ isinf(::TimeType) = false @test_throws DomainError convert(Int, Interval{Closed, Open}(10, 10)) @test convert(Int, Interval{Closed, Closed}(10, 10)) == 10 @test_throws DomainError convert(Int, Interval{Closed, Closed}(10, 11)) + @test convert(Interval{Float64, Closed, Closed}, Interval(1,2)) == Interval{Float64, Closed, Closed}(1.0,2.0) for T in (Date, DateTime) dt = T(2013, 2, 13) @@ -101,6 +102,20 @@ isinf(::TimeType) = false @test eltype(Interval{Float64}(1,2)) == Float64 end + @testset "promotion" begin + for (a1, b1, _) in test_values + for (a2, b2, _) in test_values + for (L1, R1) in BOUND_PERMUTATIONS + for (L2, R2) in BOUND_PERMUTATIONS + interval1 = Interval{L1, R1}(a1, b1) + interval2 = Interval{L2, R2}(a2, b2) + @test promote(interval1, interval2) == (Interval{L1,R1}(promote(a1, b1)...), Interval{L2,R2}(promote(a2, b2)...)) + end + end + end + end + end + @testset "accessors" begin for (a, b, _) in test_values for (L, R) in BOUND_PERMUTATIONS