diff --git a/Project.toml b/Project.toml index 0d4cefe..2d34bb0 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "IntervalSets" uuid = "8197267c-284f-5f27-9208-e0e47529a953" -version = "0.7.7" +version = "0.7.8" [deps] Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" diff --git a/src/IntervalSets.jl b/src/IntervalSets.jl index 516dbb3..80bff29 100644 --- a/src/IntervalSets.jl +++ b/src/IntervalSets.jl @@ -1,7 +1,6 @@ module IntervalSets -using Base: @pure -import Base: eltype, convert, show, in, length, isempty, isequal, isapprox, issubset, ==, hash, +import Base: convert, show, in, length, isempty, isequal, isapprox, issubset, ==, hash, union, intersect, minimum, maximum, extrema, range, clamp, mod, float, ⊇, ⊊, ⊋ using Random @@ -62,8 +61,17 @@ isclosedset(d::AbstractInterval) = isleftclosed(d) && isrightclosed(d) "Is the interval open?" isopenset(d::AbstractInterval) = isleftopen(d) && isrightopen(d) -eltype(::Type{AbstractInterval{T}}) where {T} = T -@pure eltype(::Type{I}) where {I<:AbstractInterval} = eltype(supertype(I)) +boundstype(i::AbstractInterval) = boundstype(typeof(i)) +boundstype(::Type{I}) where {I<:AbstractInterval{T}} where T = T + +@static if VERSION < v"1.9" + function Base.eltype(I::Type{<:AbstractInterval}) + Base.depwarn("`eltype` for `AbstractInterval` will be replaced with `boundstype` in the next breaking release (v0.8.0).", :eltype) + boundstype(I) + end +else + @deprecate Base.eltype(I::Type{<:AbstractInterval}) boundstype(I) false +end convert(::Type{AbstractInterval}, i::AbstractInterval) = i convert(::Type{AbstractInterval{T}}, i::AbstractInterval{T}) where T = i @@ -141,7 +149,7 @@ isequal(A::TypedEndpointsInterval, B::TypedEndpointsInterval) = isempty(A) & ise ==(A::TypedEndpointsInterval{L,R}, B::TypedEndpointsInterval{L,R}) where {L,R} = (leftendpoint(A) == leftendpoint(B) && rightendpoint(A) == rightendpoint(B)) || (isempty(A) && isempty(B)) ==(A::TypedEndpointsInterval, B::TypedEndpointsInterval) = isempty(A) && isempty(B) -function isapprox(A::AbstractInterval, B::AbstractInterval; atol=0, rtol=Base.rtoldefault(eltype(A), eltype(B), atol), kwargs...) +function isapprox(A::AbstractInterval, B::AbstractInterval; atol=0, rtol=Base.rtoldefault(boundstype(A), boundstype(B), atol), kwargs...) closedendpoints(A) != closedendpoints(B) && error("Comparing intervals with different closedness is not defined") isempty(A) != isempty(B) && return false isempty(A) && isempty(B) && return true diff --git a/test/runtests.jl b/test/runtests.jl index e36a964..51dd71a 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -6,7 +6,7 @@ import Statistics: mean using Random using Unitful -import IntervalSets: Domain, endpoints, closedendpoints, TypedEndpointsInterval +import IntervalSets: Domain, endpoints, closedendpoints, TypedEndpointsInterval, boundstype struct MyClosedUnitInterval <: TypedEndpointsInterval{:closed,:closed,Int} end endpoints(::MyClosedUnitInterval) = (0,1) @@ -54,6 +54,8 @@ struct IncompleteInterval <: AbstractInterval{Int} end O = @inferred x±y @test O == ClosedInterval(x-y, x+y) + @test boundstype(I) == Int + @test boundstype(M) == Float64 @test eltype(I) == Int @test eltype(M) == Float64 @@ -642,6 +644,7 @@ struct IncompleteInterval <: AbstractInterval{Int} end @testset "Custom intervals" begin I = MyUnitInterval(true,true) + @test boundstype(I) == boundstype(typeof(I)) == Int @test eltype(I) == eltype(typeof(I)) == Int @test leftendpoint(I) == 0 @test rightendpoint(I) == 1 @@ -822,6 +825,7 @@ struct IncompleteInterval <: AbstractInterval{Int} end @testset "IncompleteInterval" begin I = IncompleteInterval() + @test boundstype(I) === Int @test eltype(I) === Int @test_throws ErrorException endpoints(I) @test_throws ErrorException closedendpoints(I)