Skip to content

Commit

Permalink
Replace convert for scalars
Browse files Browse the repository at this point in the history
  • Loading branch information
pabloferz committed Oct 8, 2020
1 parent c37cbb7 commit f3165b3
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 34 deletions.
1 change: 1 addition & 0 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ Open
Unbounded
first
last
scalar
span
isclosed
isopen
Expand Down
1 change: 1 addition & 0 deletions src/Intervals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export Bound,
HB,
first,
last,
scalar,
span,
bounds_types,
isclosed,
Expand Down
21 changes: 7 additions & 14 deletions src/anchoredinterval.jl
Original file line number Diff line number Diff line change
Expand Up @@ -176,23 +176,16 @@ 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.convert(::Type{T}, interval::AnchoredInterval{P,T}) where {P,T}
"""
scalar(::AnchoredInterval{P, T}) -> T
Converts a closed interval set with a single element into a scalar.
"""
function scalar(interval::AnchoredInterval{P}) where {P}
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"))
throw(DomainError(interval, "The interval is not closed with coinciding endpoints"))
end
end

Expand Down
9 changes: 6 additions & 3 deletions src/interval.jl
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,12 @@ 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
"""
scalar(::Interval{T}) -> T
Converts a closed interval set with a single element into a scalar.
"""
function scalar(interval::Interval)
if first(interval) == last(interval) && isclosed(interval)
return first(interval)
else
Expand Down
10 changes: 3 additions & 7 deletions test/anchoredinterval.jl
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,13 @@ using Intervals: Bounded, Ending, Beginning, canonicalize, isunbounded

@testset "conversion" begin
interval = AnchoredInterval{Hour(0)}(dt)
@test convert(DateTime, interval) == dt
@test scalar(interval) == dt

he = HourEnding(dt)
hb = HourBeginning(dt)

# Note: When the deprecation is dropped remove the deprecated tests and uncomment
# the DomainError tests
@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_throws DomainError scalar(he)
@test_throws DomainError scalar(hb)

@test convert(Interval, he) == Interval{Open, Closed}(dt - Hour(1), dt)
@test convert(Interval, hb) == Interval{Closed, Open}(dt, dt + Hour(1))
Expand Down
20 changes: 10 additions & 10 deletions test/interval.jl
Original file line number Diff line number Diff line change
Expand Up @@ -98,19 +98,19 @@
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 scalar(Interval{Open, Open}(10, 10))
@test_throws DomainError scalar(Interval{Open, Closed}(10, 10))
@test_throws DomainError scalar(Interval{Closed, Open}(10, 10))
@test scalar(Interval{Closed, Closed}(10, 10)) == 10
@test_throws DomainError scalar(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 scalar(Interval{Open, Open}(dt, dt))
@test_throws DomainError scalar(Interval{Open, Closed}(dt, dt))
@test_throws DomainError scalar(Interval{Closed, Open}(dt, dt))
@test scalar(Interval{Closed, Closed}(dt, dt)) == dt
@test_throws DomainError scalar(Interval{Closed, Closed}(dt, dt + Day(1)))
end
end

Expand Down

0 comments on commit f3165b3

Please sign in to comment.