-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Additional accessor and convert functions #108
base: master
Are you sure you want to change the base?
Changes from all commits
e39e57f
b159390
852227f
130d689
7266148
6cf3009
a270c77
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -189,6 +189,14 @@ function Base.convert(::Type{T}, interval::Interval{T}) where T | |
end | ||
end | ||
|
||
function Base.convert(::Type{<:Interval{T}}, interval::Interval{S,L,R}) where {T,S,L,R} | ||
return Interval{T,L,R}(first(interval), last(interval)) | ||
end | ||
|
||
Base.promote_rule(::Type{Interval{T,L1,R1}}, ::Type{Interval{S,L2,R2}}) where {T,S,L1,R1,L2,R2} = Interval{promote_type(T,S), <:Union{L1,L2}, <:Union{R1,R2}} | ||
# hacky way to fix situations where T and S are the same but L and R are different | ||
Base.not_sametype(x::Tuple{Interval{T,L1,R1}, Interval{T,L2,R2}}, y::Tuple{Interval{T,L1,R1}, Interval{T,L2,R2}}) where {T,L1,R1,L2,R2} = nothing | ||
Comment on lines
+196
to
+198
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably we should just define: Base.promote_rule(::Type{Interval{T,L,R}}, ::Type{Interval{S,L,R}}) where {T,S,L,R} = Interval{promote_type(T,S), L, R} I suspect this would eliminate the need for the hack |
||
|
||
##### DISPLAY ##### | ||
|
||
function Base.show(io::IO, interval::Interval{T,L,R}) where {T,L,R} | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -85,6 +85,7 @@ isinf(::TimeType) = false | |||||||||||||||||
@test_throws DomainError convert(Int, Interval{Closed, Open}(10, 10)) | ||||||||||||||||||
@test convert(Int, Interval{Closed, Closed}(10, 10)) == 10 | ||||||||||||||||||
@test_throws DomainError convert(Int, Interval{Closed, Closed}(10, 11)) | ||||||||||||||||||
@test convert(Interval{Float64, Closed, Closed}, Interval(1,2)) == Interval{Float64, Closed, Closed}(1.0,2.0) | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmmm, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it also be useful to include There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this would make more sense as a constructor: https://docs.julialang.org/en/v1/manual/conversion-and-promotion/#Conversion-vs.-Construction-1. I'd probably leave this out unless you have a use case. |
||||||||||||||||||
|
||||||||||||||||||
for T in (Date, DateTime) | ||||||||||||||||||
dt = T(2013, 2, 13) | ||||||||||||||||||
|
@@ -101,6 +102,20 @@ isinf(::TimeType) = false | |||||||||||||||||
@test eltype(Interval{Float64}(1,2)) == Float64 | ||||||||||||||||||
end | ||||||||||||||||||
|
||||||||||||||||||
@testset "promotion" begin | ||||||||||||||||||
for (a1, b1, _) in test_values | ||||||||||||||||||
for (a2, b2, _) in test_values | ||||||||||||||||||
for (L1, R1) in BOUND_PERMUTATIONS | ||||||||||||||||||
for (L2, R2) in BOUND_PERMUTATIONS | ||||||||||||||||||
interval1 = Interval{L1, R1}(a1, b1) | ||||||||||||||||||
interval2 = Interval{L2, R2}(a2, b2) | ||||||||||||||||||
@test promote(interval1, interval2) == (Interval{L1,R1}(promote(a1, b1)...), Interval{L2,R2}(promote(a2, b2)...)) | ||||||||||||||||||
Comment on lines
+110
to
+112
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||
end | ||||||||||||||||||
end | ||||||||||||||||||
end | ||||||||||||||||||
end | ||||||||||||||||||
end | ||||||||||||||||||
|
||||||||||||||||||
@testset "accessors" begin | ||||||||||||||||||
for (a, b, _) in test_values | ||||||||||||||||||
for (L, R) in BOUND_PERMUTATIONS | ||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.