From d912e21f29089054bac0c3b3cf1ade91029e5854 Mon Sep 17 00:00:00 2001 From: Pablo Zubieta <8410335+pabloferz@users.noreply.github.com> Date: Tue, 6 Oct 2020 23:20:23 -0500 Subject: [PATCH] Add only as replacement for convert --- docs/src/index.md | 1 + src/anchoredinterval.jl | 27 +++++++++++++++------------ src/interval.jl | 14 ++++++++++---- test/anchoredinterval.jl | 9 ++++----- test/interval.jl | 23 +++++++++++++---------- 5 files changed, 43 insertions(+), 31 deletions(-) diff --git a/docs/src/index.md b/docs/src/index.md index c7bb368e..9f1951ec 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -263,6 +263,7 @@ Open Unbounded first last +only span isclosed isopen diff --git a/src/anchoredinterval.jl b/src/anchoredinterval.jl index b406cc37..0363dfb6 100644 --- a/src/anchoredinterval.jl +++ b/src/anchoredinterval.jl @@ -176,23 +176,26 @@ span(interval::AnchoredInterval{P}) where P = abs(P) ##### CONVERSION ##### -# Allows an interval to be converted to a scalar when the set contained by the interval only -# contains a single element. +function Base.only(interval::AnchoredInterval{P}) where {P} + if isclosed(interval) && (sign(P) == 0 || first(interval) == last(interval)) + return first(interval) + else + throw(DomainError(interval, "The interval is not closed with coinciding endpoints")) + end +end + +# Remove in version 2.0.0 function Base.convert(::Type{T}, interval::AnchoredInterval{P,T}) where {P,T} + depwarn( + "`convert(::Type{T}, interval::AnchoredInterval{P, T})` is deprecated, " * + "use `only(interval::AnchoredInterval{P,T})` for closed intervals with " * + "coinciding endpoints or `anchor(interval)` otherwise.", + :convert, + ) if isclosed(interval) && (sign(P) == 0 || first(interval) == last(interval)) return first(interval) else - # Remove deprecation in version 2.0.0 - depwarn( - "`convert(T, interval::AnchoredInterval{P,T})` is deprecated for " * - "intervals which are not closed with coinciding endpoints. " * - "Use `anchor(interval)` instead.", - :convert, - ) return anchor(interval) - - # TODO: For when deprecation is removed - # throw(DomainError(interval, "The interval is not closed with coinciding endpoints")) end end diff --git a/src/interval.jl b/src/interval.jl index a674c2e6..96aa9eed 100644 --- a/src/interval.jl +++ b/src/interval.jl @@ -235,9 +235,7 @@ end ##### CONVERSION ##### -# Allows an interval to be converted to a scalar when the set contained by the interval only -# contains a single element. -function Base.convert(::Type{T}, interval::Interval{T}) where T +function Base.only(interval::Interval) if first(interval) == last(interval) && isclosed(interval) return first(interval) else @@ -245,8 +243,16 @@ function Base.convert(::Type{T}, interval::Interval{T}) where T end end -##### DISPLAY ##### +function Base.convert(::Type{T}, interval::Interval{T}) where {T} + depwarn( + "`convert(::Type{T}, interval::Interval{T})` is deprecated, " * + "use `only(interval)` instead.", + :convert, + ) + return only(interval) +end +##### DISPLAY ##### function Base.show(io::IO, interval::Interval{T,L,R}) where {T,L,R} if get(io, :compact, false) diff --git a/test/anchoredinterval.jl b/test/anchoredinterval.jl index 90858815..41fa85f6 100644 --- a/test/anchoredinterval.jl +++ b/test/anchoredinterval.jl @@ -114,17 +114,16 @@ using Intervals: Bounded, Ending, Beginning, canonicalize, isunbounded @testset "conversion" begin interval = AnchoredInterval{Hour(0)}(dt) - @test convert(DateTime, interval) == dt + @test only(interval) == dt he = HourEnding(dt) hb = HourBeginning(dt) - # Note: When the deprecation is dropped remove the deprecated tests and uncomment - # the DomainError tests + @test_throws DomainError only(he) + @test_throws DomainError only(hb) + @test (@test_deprecated convert(DateTime, he)) == anchor(he) @test (@test_deprecated convert(DateTime, hb)) == anchor(hb) - # @test_throws DomainError convert(DateTime, he) - # @test_throws DomainError convert(DateTime, hb) @test convert(Interval, he) == Interval{Open, Closed}(dt - Hour(1), dt) @test convert(Interval, hb) == Interval{Closed, Open}(dt, dt + Hour(1)) diff --git a/test/interval.jl b/test/interval.jl index 0025e14e..a017b47a 100644 --- a/test/interval.jl +++ b/test/interval.jl @@ -98,19 +98,22 @@ end @testset "conversion" begin - @test_throws DomainError convert(Int, Interval{Open, Open}(10, 10)) - @test_throws DomainError convert(Int, Interval{Open, Closed}(10, 10)) - @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_throws DomainError only(Interval{Open, Open}(10, 10)) + @test_throws DomainError only(Interval{Open, Closed}(10, 10)) + @test_throws DomainError only(Interval{Closed, Open}(10, 10)) + @test only(Interval{Closed, Closed}(10, 10)) == 10 + @test_throws DomainError only(Interval{Closed, Closed}(10, 11)) + + @test (@test_deprecated convert(Int, Interval{Closed, Closed}(10, 10))) == 10 + @test_deprecated (@test_throws DomainError convert(Int, Interval{Closed, Closed}(10, 11))) for T in (Date, DateTime) dt = T(2013, 2, 13) - @test_throws DomainError convert(T, Interval{Open, Open}(dt, dt)) - @test_throws DomainError convert(T, Interval{Open, Closed}(dt, dt)) - @test_throws DomainError convert(T, Interval{Closed, Open}(dt, dt)) - @test convert(T, Interval{Closed, Closed}(dt, dt)) == dt - @test_throws DomainError convert(T, Interval{Closed, Closed}(dt, dt + Day(1))) + @test_throws DomainError only(Interval{Open, Open}(dt, dt)) + @test_throws DomainError only(Interval{Open, Closed}(dt, dt)) + @test_throws DomainError only(Interval{Closed, Open}(dt, dt)) + @test only(Interval{Closed, Closed}(dt, dt)) == dt + @test_throws DomainError only(Interval{Closed, Closed}(dt, dt + Day(1))) end end