Skip to content
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

Deprecate methods for eltype and add boundstype #150

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
18 changes: 13 additions & 5 deletions src/IntervalSets.jl
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down