Skip to content

Commit

Permalink
Allow in with mixed types (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
omus authored Jan 15, 2019
1 parent 4bee459 commit 46ec002
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/interval.jl
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,12 @@ true
##### SET OPERATIONS #####

Base.isempty(i::AbstractInterval) = LeftEndpoint(i) > RightEndpoint(i)
Base.in(a::T, b::AbstractInterval{T}) where T = !(a b || a b)
Base.in(a, b::AbstractInterval) = !(a b || a b)

function Base.in(a::AbstractInterval, b::AbstractInterval)
# Intervals should be compared with set operations
throw(ArgumentError("Intervals can not be compared with `in`. Use `issubset` instead."))
end

function Base.issubset(a::AbstractInterval, b::AbstractInterval)
return LeftEndpoint(a) LeftEndpoint(b) && RightEndpoint(a) RightEndpoint(b)
Expand Down
3 changes: 2 additions & 1 deletion test/interval.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
@testset "Interval" begin
test_values = [
(-10, 1000, 1),
(0.0, 1, 0.01), # Use different types to test promotion
('a', 'z', 1),
(Date(2013, 2, 13), Date(2013, 3, 13), Day(1)),
(DateTime(2016, 8, 11, 0, 30), DateTime(2016, 8, 11, 1), Millisecond(1))
Expand Down Expand Up @@ -384,7 +385,7 @@
@test in(b - unit, interval)
@test !in(b + unit, interval)

@test_throws MethodError (in(Interval(a, b), Interval(a, b)))
@test_throws ArgumentError (in(Interval(a, b), Interval(a, b)))
end
end

Expand Down

0 comments on commit 46ec002

Please sign in to comment.