From 81c4663909b9ffaaacb552ca9d14f3ee079a877b Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Wed, 10 Jun 2020 11:54:09 -0500 Subject: [PATCH] Throw exception when calculating unbounded span --- src/anchoredinterval.jl | 9 ++++++++- src/interval.jl | 10 +++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/anchoredinterval.jl b/src/anchoredinterval.jl index 9a66d4a0..da98e9d8 100644 --- a/src/anchoredinterval.jl +++ b/src/anchoredinterval.jl @@ -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 ##### diff --git a/src/interval.jl b/src/interval.jl index 99aa1ef4..6c4af654 100644 --- a/src/interval.jl +++ b/src/interval.jl @@ -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))