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 1 commit
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
Prev Previous commit
Handle StackOverflow error with Any
  • Loading branch information
omus committed Nov 29, 2017
commit 6477256b12c95d956c7717f3896e3c4a43b49527
9 changes: 6 additions & 3 deletions src/Missings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,12 @@ for f in (:(!), :(+), :(-), :(Base.identity), :(Base.zero), :(Base.one), :(Base.
@eval $(f)(d::Missing) = missing
end

Base.zero(::Type{Union{T, Missing}}) where T = zero(T)
Base.one(::Type{Union{T, Missing}}) where T = one(T)
Base.oneunit(::Type{Union{T, Missing}}) where T = oneunit(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
5 changes: 5 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ using Base.Test, Missings, Compat
@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