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

Support one and oneunit #59

Merged
merged 2 commits into from
Dec 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The type restriction was there on purpose. See #44, and the workaround by defining zero(::Type{Any}) here (but this would be type piracy in this package).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing this out. I think I found a nice way to work around the type piracy problem.

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