Skip to content

Commit

Permalink
Support one and oneunit (#59)
Browse files Browse the repository at this point in the history
* Support one and oneunit

* Handle StackOverflow error with Any
  • Loading branch information
omus authored and ararslan committed Dec 1, 2017
1 parent 4287e00 commit edd1306
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
10 changes: 7 additions & 3 deletions src/Missings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ if VERSION < v"0.7.0-DEV.300"
end

# Unary operators/functions
for f in (:(!), :(+), :(-), :(Base.identity), :(Base.zero),
for f in (:(!), :(+), :(-), :(Base.identity), :(Base.zero), :(Base.one), :(Base.oneunit),
:(Base.abs), :(Base.abs2), :(Base.sign),
:(Base.acos), :(Base.acosh), :(Base.asin), :(Base.asinh), :(Base.atan), :(Base.atanh),
:(Base.sin), :(Base.sinh), :(Base.cos), :(Base.cosh), :(Base.tan), :(Base.tanh),
Expand All @@ -95,8 +95,12 @@ for f in (:(!), :(+), :(-), :(Base.identity), :(Base.zero),
@eval $(f)(d::Missing) = missing
end

Base.zero(::Type{Union{T, Missing}}) where {T <: Number} = zero(T)
Base.zero(::Type{Union{T, Missing}}) where {T <: Compat.Dates.Period} = zero(T)
for f in (:(Base.zero), :(Base.one), :(Base.oneunit))
@eval function $(f)(::Type{Union{T, Missing}}) where T
T === Any && throw(MethodError($f, (Any,)))
$f(T)
end
end

# Binary operators/functions
for f in (:(+), :(-), :(*), :(/), :(^),
Expand Down
16 changes: 13 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ using Base.Test, Missings, Compat
conj, cos, cosh, tan, tanh,
exp, exp2, expm1, log, log10, log1p, log2,
exponent, sqrt, gamma, lgamma,
identity, zero,
identity, zero, one, oneunit,
iseven, isodd, ispow2,
isfinite, isinf, isnan, iszero,
isinteger, isreal, isimag,
Expand All @@ -55,14 +55,24 @@ using Base.Test, Missings, Compat
@test_throws MissingException f(Int, missing)
end

@test zero(Union{Int, Missing}) === 0
@test zero(Union{Float64, Missing}) === 0.0
for T in (Int, Float64)
@test zero(Union{T, Missing}) === T(0)
@test one(Union{T, Missing}) === T(1)
@test oneunit(Union{T, Missing}) === T(1)
end

for T in (subtypes(Compat.Dates.DatePeriod)...,
subtypes(Compat.Dates.TimePeriod)...)
@test zero(Union{T, Missing}) === T(0)
@test one(Union{T, Missing}) === 1
@test oneunit(Union{T, Missing}) === T(1)
end

# Make sure we didn't introduce a StackOverflow error
@test_throws MethodError zero(Any)
@test_throws MethodError one(Any)
@test_throws MethodError oneunit(Any)

# Comparison operators
@test (missing == missing) === missing
@test (1 == missing) === missing
Expand Down

0 comments on commit edd1306

Please sign in to comment.