diff --git a/base/checked.jl b/base/checked.jl index 7c6a401fa6337..0677aa2de3089 100644 --- a/base/checked.jl +++ b/base/checked.jl @@ -4,8 +4,6 @@ module Checked -import Base: checked_neg, checked_abs, checked_add, checked_sub, checked_mul, - checked_div, checked_rem, checked_fld, checked_mod, checked_cld export checked_neg, checked_abs, checked_add, checked_sub, checked_mul, checked_div, checked_rem, checked_fld, checked_mod, checked_cld @@ -15,6 +13,29 @@ import Core.Intrinsics: box, unbox, checked_uadd_int, checked_usub_int, checked_umul_int, checked_udiv_int, checked_urem_int +# define promotion behavior for checked operations +checked_add(x::Integer, y::Integer) = checked_add(promote(x,y)...) +checked_sub(x::Integer, y::Integer) = checked_sub(promote(x,y)...) +checked_mul(x::Integer, y::Integer) = checked_mul(promote(x,y)...) +checked_div(x::Integer, y::Integer) = checked_div(promote(x,y)...) +checked_rem(x::Integer, y::Integer) = checked_rem(promote(x,y)...) +checked_fld(x::Integer, y::Integer) = checked_fld(promote(x,y)...) +checked_mod(x::Integer, y::Integer) = checked_mod(promote(x,y)...) +checked_cld(x::Integer, y::Integer) = checked_cld(promote(x,y)...) + +# fallback catchall rules to prevent infinite recursion if promotion succeeds, +# but no method exists to handle those types +checked_neg{T<:Integer}(x::T) = no_op_err("checked_neg", T) +checked_abs{T<:Integer}(x::T) = no_op_err("checked_abs", T) +checked_add{T<:Integer}(x::T, y::T) = no_op_err("checked_add", T) +checked_sub{T<:Integer}(x::T, y::T) = no_op_err("checked_sub", T) +checked_mul{T<:Integer}(x::T, y::T) = no_op_err("checked_mul", T) +checked_div{T<:Integer}(x::T, y::T) = no_op_err("checked_div", T) +checked_rem{T<:Integer}(x::T, y::T) = no_op_err("checked_rem", T) +checked_fld{T<:Integer}(x::T, y::T) = no_op_err("checked_fld", T) +checked_mod{T<:Integer}(x::T, y::T) = no_op_err("checked_mod", T) +checked_cld{T<:Integer}(x::T, y::T) = no_op_err("checked_cld", T) + typealias SignedInt Union{Int8,Int16,Int32,Int64,Int128} typealias UnsignedInt Union{UInt8,UInt16,UInt32,UInt64,UInt128} diff --git a/base/coreimg.jl b/base/coreimg.jl index 5a91910ff7c41..97853f0c56128 100644 --- a/base/coreimg.jl +++ b/base/coreimg.jl @@ -40,8 +40,8 @@ include("number.jl") include("int.jl") include("operators.jl") include("pointer.jl") -checked_add{T<:Integer}(x::T, y::T) = x+y -checked_sub{T<:Integer}(x::T, y::T) = x-y +const checked_add = + +const checked_sub = - # core array operations include("abstractarray.jl") diff --git a/base/dates/types.jl b/base/dates/types.jl index 8da30f5a4a31c..cbb9a1ed86f07 100644 --- a/base/dates/types.jl +++ b/base/dates/types.jl @@ -49,13 +49,20 @@ abstract TimeType <: AbstractTime # DateTime is a millisecond precision UTInstant interpreted by ISOCalendar immutable DateTime <: TimeType instant::UTInstant{Millisecond} + DateTime(instant::UTInstant{Millisecond}) = new(instant) end # DateTime is a day precision UTInstant interpreted by ISOCalendar immutable Date <: TimeType instant::UTInstant{Day} + Date(instant::UTInstant{Day}) = new(instant) end +# Fallback constructors +_c(x) = convert(Int64,x) +DateTime(y,m=1,d=1,h=0,mi=0,s=0,ms=0) = DateTime(_c(y),_c(m),_c(d),_c(h),_c(mi),_c(s),_c(ms)) +Date(y,m=1,d=1) = Date(_c(y),_c(m),_c(d)) + # Convert y,m,d to # of Rata Die days # Works by shifting the beginning of the year to March 1, # so a leap day is the very last day of the year @@ -154,11 +161,6 @@ function Date(periods::Period...) return Date(y,m,d) end -# Fallback constructors -_c(x) = convert(Int64,x) -DateTime(y,m=1,d=1,h=0,mi=0,s=0,ms=0) = DateTime(_c(y),_c(m),_c(d),_c(h),_c(mi),_c(s),_c(ms)) -Date(y,m=1,d=1) = Date(_c(y),_c(m),_c(d)) - # Traits, Equality Base.isfinite{T<:TimeType}(::Union{Type{T},T}) = true calendar(dt::DateTime) = ISOCalendar diff --git a/base/promotion.jl b/base/promotion.jl index 37eab035949c8..b71ed7c4d5191 100644 --- a/base/promotion.jl +++ b/base/promotion.jl @@ -200,15 +200,6 @@ max(x::Real, y::Real) = max(promote(x,y)...) min(x::Real, y::Real) = min(promote(x,y)...) minmax(x::Real, y::Real) = minmax(promote(x, y)...) -checked_add(x::Integer, y::Integer) = checked_add(promote(x,y)...) -checked_sub(x::Integer, y::Integer) = checked_sub(promote(x,y)...) -checked_mul(x::Integer, y::Integer) = checked_mul(promote(x,y)...) -checked_div(x::Integer, y::Integer) = checked_div(promote(x,y)...) -checked_rem(x::Integer, y::Integer) = checked_rem(promote(x,y)...) -checked_fld(x::Integer, y::Integer) = checked_fld(promote(x,y)...) -checked_mod(x::Integer, y::Integer) = checked_mod(promote(x,y)...) -checked_cld(x::Integer, y::Integer) = checked_cld(promote(x,y)...) - # "Promotion" that takes a Functor into account. You can override this # as needed. For example, if you need to provide a custom result type # for the multiplication of two types, @@ -249,14 +240,3 @@ minmax(x::Real) = (x, x) max{T<:Real}(x::T, y::T) = ifelse(y < x, x, y) min{T<:Real}(x::T, y::T) = ifelse(y < x, y, x) minmax{T<:Real}(x::T, y::T) = y < x ? (y, x) : (x, y) - -checked_neg{T<:Integer}(x::T) = no_op_err("checked_neg", T) -checked_abs{T<:Integer}(x::T) = no_op_err("checked_abs", T) -checked_add{T<:Integer}(x::T, y::T) = no_op_err("checked_add", T) -checked_sub{T<:Integer}(x::T, y::T) = no_op_err("checked_sub", T) -checked_mul{T<:Integer}(x::T, y::T) = no_op_err("checked_mul", T) -checked_div{T<:Integer}(x::T, y::T) = no_op_err("checked_div", T) -checked_rem{T<:Integer}(x::T, y::T) = no_op_err("checked_rem", T) -checked_fld{T<:Integer}(x::T, y::T) = no_op_err("checked_fld", T) -checked_mod{T<:Integer}(x::T, y::T) = no_op_err("checked_mod", T) -checked_cld{T<:Integer}(x::T, y::T) = no_op_err("checked_cld", T)