Skip to content
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

Add a contract for Ordered and additional traits #37239

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
17 changes: 14 additions & 3 deletions base/traits.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,26 @@
## numeric/object traits
# trait for objects that have an ordering
bkamins marked this conversation as resolved.
Show resolved Hide resolved
abstract type OrderStyle end

"""
Ordered()

Indicate that a type `T` guarantees that a method at most as specific as
`isless(::T, ::T)` is defined and when called will not throw an exception.
bkamins marked this conversation as resolved.
Show resolved Hide resolved
"""
bkamins marked this conversation as resolved.
Show resolved Hide resolved
struct Ordered <: OrderStyle end
struct Unordered <: OrderStyle end
bkamins marked this conversation as resolved.
Show resolved Hide resolved

OrderStyle(instance) = OrderStyle(typeof(instance))
bkamins marked this conversation as resolved.
Show resolved Hide resolved
OrderStyle(::Type{<:Real}) = Ordered()
OrderStyle(::Type{<:AbstractString}) = Ordered()
OrderStyle(::Type{Union{}}) = Ordered()
OrderStyle(::Type{<:Union{Missing,Real}}) = Ordered()
OrderStyle(::Type{<:Union{Missing,AbstractString}}) = Ordered()
OrderStyle(::Type{<:Union{Missing,AbstractChar}}) = Ordered()
OrderStyle(::Type{<:Union{Missing,Symbol}}) = Ordered()
OrderStyle(::Type{<:Union{Missing,CartesianIndex}}) = Ordered()
bkamins marked this conversation as resolved.
Show resolved Hide resolved
OrderStyle(::Type{Symbol}) = Ordered()
OrderStyle(::Type{Missing}) = Ordered()
bkamins marked this conversation as resolved.
Show resolved Hide resolved
OrderStyle(::Type{<:Any}) = Unordered()
OrderStyle(::Type{Union{}}) = Ordered()

# trait for objects that support arithmetic
abstract type ArithmeticStyle end
Expand Down
1 change: 1 addition & 0 deletions stdlib/Dates/src/periods.jl
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ default(p::Union{T,Type{T}}) where {T<:TimePeriod} = T(0)
==(x::Period, y::Period) = (==)(promote(x, y)...)
Base.isless(x::P, y::P) where {P<:Period} = isless(value(x), value(y))
Base.isless(x::Period, y::Period) = isless(promote(x, y)...)
Base.OrderStyle(::Type{<:Union{Missing,Period}}) = Base.Ordered()

# Period Arithmetic, grouped by dimensionality:
for op in (:+, :-, :lcm, :gcd)
Expand Down
1 change: 1 addition & 0 deletions stdlib/Dates/src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@ Base.typemin(::Union{Time, Type{Time}}) = Time(0)
Base.promote_rule(::Type{Date}, x::Type{DateTime}) = DateTime
Base.isless(x::T, y::T) where {T<:TimeType} = isless(value(x), value(y))
Base.isless(x::TimeType, y::TimeType) = isless(promote(x, y)...)
Base.OrderStyle(::Type{<:Union{Missing,TimeType}}) = Base.Ordered()
(==)(x::T, y::T) where {T<:TimeType} = (==)(value(x), value(y))
(==)(x::TimeType, y::TimeType) = (===)(promote(x, y)...)
Base.min(x::AbstractTime) = x
Expand Down
1 change: 1 addition & 0 deletions stdlib/Test/src/Test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1647,6 +1647,7 @@ struct GenericOrder{T}
val::T
end
Base.isless(x::GenericOrder, y::GenericOrder) = isless(x.val,y.val)
Base.OrderStyle(::Type{<:Union{Missing,Test.GenericOrder}}) = Base.Ordered()
bkamins marked this conversation as resolved.
Show resolved Hide resolved

Base.keys(a::GenericArray) = keys(a.a)
Base.axes(a::GenericArray) = axes(a.a)
Expand Down