Skip to content

Fix type promotion #93

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 15, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions src/interval.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,7 @@ mathematical notation, the constructed range is `(left, right)`.
const OpenInterval{T} = Interval{:open,:open,T}

Interval{L,R,T}(i::AbstractInterval) where {L,R,T} = Interval{L,R,T}(endpoints(i)...)
function Interval{L,R}(left, right) where {L,R}
# TODO: Replace the retrun value with `Interval{L,R}(promote(left,right)...)`. (#93)
T = promote_type(typeof(left), typeof(right))
if !isconcretetype(T)
Base.depwarn("`Promotion to a concrete type failed and will error in the next release; consider constructing this interval as `Interval{L,R,$(typejoin(typeof(left),typeof(right)))}(left, right)`.", :Interval)
end
Interval{L,R,T}(left,right)
end
Interval{L,R}(left, right) where {L,R} = Interval{L,R}(promote(left,right)...)
Interval{L,R}(left::T, right::T) where {L,R,T} = Interval{L,R,T}(left, right)
Interval(left, right) = ClosedInterval(left, right)

Expand Down
19 changes: 11 additions & 8 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ using Dates
using Statistics
import Statistics: mean
using Random
using Unitful

import IntervalSets: Domain, endpoints, closedendpoints, TypedEndpointsInterval

Expand All @@ -22,14 +23,15 @@ closedendpoints(I::MyUnitInterval) = (I.isleftclosed,I.isrightclosed)
struct IncompleteInterval <: AbstractInterval{Int} end

@testset "IntervalSets" begin
@test isempty(detect_ambiguities(IntervalSets, Base, Core))
@test isempty(detect_ambiguities(IntervalSets))

@test ordered(2, 1) == (1, 2)
@test ordered(1, 2) == (1, 2)
@test ordered(Float16(1), 2) == (1, 2)

@testset "Basic Closed Sets" begin
@test_throws ArgumentError :a .. "b"
@test_throws ErrorException :a .. "b"
@test_throws ErrorException 1 .. missing
I = 0..3
@test I === ClosedInterval(0,3) === ClosedInterval{Int}(0,3) ===
Interval(0,3)
Expand Down Expand Up @@ -139,6 +141,13 @@ struct IncompleteInterval <: AbstractInterval{Int} end
@test_throws MethodError duration(1.2..2.4)
end

@testset "Unitful interval" begin
@test 1.5u"m" in 1u"m" .. 2u"m"
@test 1500u"μm" in 1u"mm" .. 1u"m"
@test !(500u"μm" in 1u"mm" .. 1u"m")
@test 1u"m" .. 2u"m" == 1000u"mm" .. 2000u"mm"
end

@testset "Day interval" begin
A = Date(1990, 1, 1); B = Date(1990, 3, 1)
@test width(ClosedInterval(A, B)) == Dates.Day(59)
Expand Down Expand Up @@ -640,12 +649,6 @@ struct IncompleteInterval <: AbstractInterval{Int} end
@test I ∩ (0.0..0.5) === 0.0..0.5
end

@testset "Missing endpoints" begin
# TODO: Remove this testset in the next breaking release (#94)
@test ismissing(2 in 1..missing)
@test_broken ismissing(2 in missing..1) # would be fixed by julialang#31171
end

@testset "in" begin
@test in(0.1, 0.0..1.0) == true
@test in(0.0, 0.0..1.0) == true
Expand Down