https://github.com/JuliaLang/julia
Raw File
Tip revision: bd84fa1bad1e08b5c368999aa9b07e4382f54910 authored by Tony Kelman on 31 March 2017, 12:58:56 UTC
Tag 0.6.0-pre.beta (#21232)
Tip revision: bd84fa1
traits.jl
# This file is a part of Julia. License is MIT: http://julialang.org/license

## numeric/object traits
# trait for objects that have an ordering
abstract type TypeOrder end
struct HasOrder <: TypeOrder end
struct Unordered <: TypeOrder end

(::Type{TypeOrder})(instance) = TypeOrder(typeof(instance))
(::Type{TypeOrder})(::Type{<:Real}) = HasOrder()
(::Type{TypeOrder})(::Type{<:Any}) = Unordered()

# trait for objects that support arithmetic
abstract type TypeArithmetic end
struct ArithmeticRounds <: TypeArithmetic end     # least significant bits can be lost
struct ArithmeticOverflows <: TypeArithmetic end  #  most significant bits can be lost
struct ArithmeticUnknown <: TypeArithmetic end

(::Type{TypeArithmetic})(instance) = TypeArithmetic(typeof(instance))
(::Type{TypeArithmetic})(::Type{<:AbstractFloat}) = ArithmeticRounds()
(::Type{TypeArithmetic})(::Type{<:Integer}) = ArithmeticOverflows()
(::Type{TypeArithmetic})(::Type{<:Any}) = ArithmeticUnknown()
back to top