Skip to content

Commit 7e04521

Browse files
committed
Add only as replacement for convert
1 parent 2fe33fd commit 7e04521

File tree

6 files changed

+49
-26
lines changed

6 files changed

+49
-26
lines changed

docs/src/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ Open
263263
Unbounded
264264
first
265265
last
266+
only
266267
span
267268
isclosed
268269
isopen

src/Intervals.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ using Dates: AbstractDateTime, value, coarserperiod
1010

1111
import Base: , , , , union, union!, merge
1212

13+
# Extend `Base.only` when it exists
14+
if VERSION >= v"1.4.0-DEV.142"
15+
import Base: only
16+
end
17+
1318
# Extend `Base.isdisjoint` when it exists
1419
# https://github.com/JuliaLang/julia/pull/34427
1520
if VERSION >= v"1.5.0-DEV.124"
@@ -54,6 +59,7 @@ export Bound,
5459
HB,
5560
first,
5661
last,
62+
only,
5763
span,
5864
bounds_types,
5965
isclosed,

src/anchoredinterval.jl

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -176,23 +176,31 @@ span(interval::AnchoredInterval{P}) where P = abs(P)
176176

177177
##### CONVERSION #####
178178

179-
# Allows an interval to be converted to a scalar when the set contained by the interval only
180-
# contains a single element.
179+
function only(interval::AnchoredInterval{P}) where {P}
180+
if isclosed(interval) && (sign(P) == 0 || first(interval) == last(interval))
181+
return first(interval)
182+
else
183+
throw(DomainError(interval, "The interval is not closed with coinciding endpoints"))
184+
end
185+
end
186+
187+
# Remove in version 2.0.0
181188
function Base.convert(::Type{T}, interval::AnchoredInterval{P,T}) where {P,T}
182189
if isclosed(interval) && (sign(P) == 0 || first(interval) == last(interval))
190+
depwarn(
191+
"`convert(::Type{T}, interval::AnchoredInterval{P,T})` is deprecated, " *
192+
"use `only(interval::AnchoredInterval{P,T})` instead.",
193+
:convert,
194+
)
183195
return first(interval)
184196
else
185-
# Remove deprecation in version 2.0.0
186197
depwarn(
187-
"`convert(T, interval::AnchoredInterval{P,T})` is deprecated for " *
198+
"`convert(::Type{T}, interval::AnchoredInterval{P,T})` is deprecated for " *
188199
"intervals which are not closed with coinciding endpoints. " *
189200
"Use `anchor(interval)` instead.",
190201
:convert,
191202
)
192203
return anchor(interval)
193-
194-
# TODO: For when deprecation is removed
195-
# throw(DomainError(interval, "The interval is not closed with coinciding endpoints"))
196204
end
197205
end
198206

src/interval.jl

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,18 +235,24 @@ end
235235

236236
##### CONVERSION #####
237237

238-
# Allows an interval to be converted to a scalar when the set contained by the interval only
239-
# contains a single element.
240-
function Base.convert(::Type{T}, interval::Interval{T}) where T
238+
function only(interval::Interval)
241239
if first(interval) == last(interval) && isclosed(interval)
242240
return first(interval)
243241
else
244242
throw(DomainError(interval, "The interval is not closed with coinciding endpoints"))
245243
end
246244
end
247245

248-
##### DISPLAY #####
246+
function Base.convert(::Type{T}, interval::Interval{T}) where {T}
247+
depwarn(
248+
"`convert(::Type{T}, interval::Interval{T})` is deprecated, " *
249+
"use `only(interval)` instead.",
250+
:convert,
251+
)
252+
return only(interval)
253+
end
249254

255+
##### DISPLAY #####
250256

251257
function Base.show(io::IO, interval::Interval{T,L,R}) where {T,L,R}
252258
if get(io, :compact, false)

test/anchoredinterval.jl

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,17 +114,16 @@ using Intervals: Bounded, Ending, Beginning, canonicalize, isunbounded
114114

115115
@testset "conversion" begin
116116
interval = AnchoredInterval{Hour(0)}(dt)
117-
@test convert(DateTime, interval) == dt
117+
@test only(interval) == dt
118118

119119
he = HourEnding(dt)
120120
hb = HourBeginning(dt)
121121

122-
# Note: When the deprecation is dropped remove the deprecated tests and uncomment
123-
# the DomainError tests
122+
@test_throws DomainError only(he)
123+
@test_throws DomainError only(hb)
124+
124125
@test (@test_deprecated convert(DateTime, he)) == anchor(he)
125126
@test (@test_deprecated convert(DateTime, hb)) == anchor(hb)
126-
# @test_throws DomainError convert(DateTime, he)
127-
# @test_throws DomainError convert(DateTime, hb)
128127

129128
@test convert(Interval, he) == Interval{Open, Closed}(dt - Hour(1), dt)
130129
@test convert(Interval, hb) == Interval{Closed, Open}(dt, dt + Hour(1))

test/interval.jl

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -98,19 +98,22 @@
9898
end
9999

100100
@testset "conversion" begin
101-
@test_throws DomainError convert(Int, Interval{Open, Open}(10, 10))
102-
@test_throws DomainError convert(Int, Interval{Open, Closed}(10, 10))
103-
@test_throws DomainError convert(Int, Interval{Closed, Open}(10, 10))
104-
@test convert(Int, Interval{Closed, Closed}(10, 10)) == 10
105-
@test_throws DomainError convert(Int, Interval{Closed, Closed}(10, 11))
101+
@test_throws DomainError only(Interval{Open, Open}(10, 10))
102+
@test_throws DomainError only(Interval{Open, Closed}(10, 10))
103+
@test_throws DomainError only(Interval{Closed, Open}(10, 10))
104+
@test only(Interval{Closed, Closed}(10, 10)) == 10
105+
@test_throws DomainError only(Interval{Closed, Closed}(10, 11))
106+
107+
@test (@test_deprecated convert(Int, Interval{Closed, Closed}(10, 10))) == 10
108+
@test_throws DomainError (@test_deprecated convert(Int, Interval{Closed, Closed}(10, 11)))
106109

107110
for T in (Date, DateTime)
108111
dt = T(2013, 2, 13)
109-
@test_throws DomainError convert(T, Interval{Open, Open}(dt, dt))
110-
@test_throws DomainError convert(T, Interval{Open, Closed}(dt, dt))
111-
@test_throws DomainError convert(T, Interval{Closed, Open}(dt, dt))
112-
@test convert(T, Interval{Closed, Closed}(dt, dt)) == dt
113-
@test_throws DomainError convert(T, Interval{Closed, Closed}(dt, dt + Day(1)))
112+
@test_throws DomainError only(Interval{Open, Open}(dt, dt))
113+
@test_throws DomainError only(Interval{Open, Closed}(dt, dt))
114+
@test_throws DomainError only(Interval{Closed, Open}(dt, dt))
115+
@test only(Interval{Closed, Closed}(dt, dt)) == dt
116+
@test_throws DomainError only(Interval{Closed, Closed}(dt, dt + Day(1)))
114117
end
115118
end
116119

0 commit comments

Comments
 (0)