Skip to content

Commit

Permalink
Throw exception when calculating unbounded span
Browse files Browse the repository at this point in the history
  • Loading branch information
omus committed Jun 10, 2020
1 parent 54d8f1a commit 81c4663
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/anchoredinterval.jl
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,14 @@ function Base.last(interval::AnchoredInterval{P}) where P
end

anchor(interval::AnchoredInterval) = interval.anchor
span(interval::AnchoredInterval{P}) where P = abs(P)

function span(interval::AnchoredInterval{P}) where P
if !isunbounded(interval)
abs(P)
else
throw(ArgumentError("Unable to determine the span of an unbounded interval"))
end
end

##### CONVERSION #####

Expand Down
10 changes: 9 additions & 1 deletion src/interval.jl
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,15 @@ end

Base.first(interval::Interval) = interval.first
Base.last(interval::Interval) = interval.last
span(interval::Interval) = interval.last - interval.first

function span(interval::Interval)
if !isunbounded(interval)
interval.last - interval.first
else
throw(ArgumentError("Unable to determine the span of an unbounded interval"))
end
end

inclusivity(interval::AbstractInterval{T,L,R}) where {T,L,R} = Inclusivity(L === Closed, R === Closed)
isclosed(interval::AbstractInterval) = isclosed(inclusivity(interval))
Base.isopen(interval::AbstractInterval) = isopen(inclusivity(interval))
Expand Down

0 comments on commit 81c4663

Please sign in to comment.