Skip to content

Commit 600fefc

Browse files
committed
Support range(::ClosedInterval{<:Integer}) -> UnitRange
Also restricts `convert(::Type{<:AbstractUnitRange}, i)` to `i::ClosedInterval`. That definition wouldn't make sense for an open interval.
1 parent 627590b commit 600fefc

File tree

3 files changed

+8
-4
lines changed

3 files changed

+8
-4
lines changed

src/IntervalSets.jl

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module IntervalSets
55
# package code goes here
66

77
using Base: @pure
8-
import Base: eltype, convert, show, in, length, isempty, isequal, issubset, ==, hash, union, intersect, minimum, maximum
8+
import Base: eltype, convert, show, in, length, isempty, isequal, issubset, ==, hash, union, intersect, minimum, maximum, range
99

1010
using Compat
1111

@@ -27,9 +27,6 @@ function convert{I<:AbstractInterval}(::Type{I}, r::Range)
2727
T = eltype(I)
2828
I(convert(T, minimum(r)), convert(T, maximum(r)))
2929
end
30-
function convert{R<:AbstractUnitRange,I<:Integer}(::Type{R}, i::AbstractInterval{I})
31-
R(minimum(i), maximum(i))
32-
end
3330

3431
ordered{T}(a::T, b::T) = ifelse(a < b, (a, b), (b, a))
3532
ordered(a, b) = ordered(promote(a, b)...)

src/closed.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,9 @@ function width{T}(A::ClosedInterval{T})
7676
_width = A.right - A.left
7777
max(zero(_width), _width) # this works when T is a Date
7878
end
79+
80+
function convert{R<:AbstractUnitRange,I<:Integer}(::Type{R}, i::ClosedInterval{I})
81+
R(minimum(i), maximum(i))
82+
end
83+
84+
range{I<:Integer}(i::ClosedInterval{I}) = convert(UnitRange{I}, i)

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ using Base.Test
1111
I = 0..3
1212
@test string(I) == "0..3"
1313
@test convert(UnitRange, I) === 0:3
14+
@test range(I) === 0:3
1415
@test convert(UnitRange{Int16}, I) === Int16(0):Int16(3)
1516
J = 3..2
1617
K = 5..4

0 commit comments

Comments
 (0)