Skip to content

Commit b523228

Browse files
committedNov 30, 2017
Support one and oneunit with Missing
1 parent 96eb33a commit b523228

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed
 

‎base/missing.jl

+8-5
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ isless(::Missing, ::Any) = false
4848
isless(::Any, ::Missing) = true
4949

5050
# Unary operators/functions
51-
for f in (:(!), :(+), :(-), :(identity), :(zero),
51+
for f in (:(!), :(+), :(-), :(identity), :(zero), :(one), :(oneunit),
5252
:(abs), :(abs2), :(sign),
5353
:(acos), :(acosh), :(asin), :(asinh), :(atan), :(atanh),
5454
:(sin), :(sinh), :(cos), :(cosh), :(tan), :(tanh),
@@ -60,9 +60,12 @@ for f in (:(!), :(+), :(-), :(identity), :(zero),
6060
@eval Math.$(f)(::Missing) = missing
6161
end
6262

63-
zero(::Type{Union{T, Missing}}) where {T} = zero(T)
64-
# To prevent StackOverflowError
65-
zero(::Type{Any}) = throw(MethodError(zero, (Any,)))
63+
for f in (:(Base.zero), :(Base.one), :(Base.oneunit))
64+
@eval function $(f)(::Type{Union{T, Missing}}) where T
65+
T === Any && throw(MethodError($f, (Any,))) # To prevent StackOverflowError
66+
$f(T)
67+
end
68+
end
6669

6770
# Binary operators/functions
6871
for f in (:(+), :(-), :(*), :(/), :(^),
@@ -112,4 +115,4 @@ function float(A::AbstractArray{Union{T, Missing}}) where {T}
112115
U = typeof(float(zero(T)))
113116
convert(AbstractArray{Union{U, Missing}}, A)
114117
end
115-
float(A::AbstractArray{Missing}) = A
118+
float(A::AbstractArray{Missing}) = A

‎test/missing.jl

+23-3
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,20 @@ end
105105
@test ismissing(missing * "a")
106106
end
107107

108+
# Emulate a unitful type such as Dates.Minute
109+
struct Unit
110+
value::Int
111+
end
112+
Base.zero(::Type{Unit}) = Unit(0)
113+
Base.one(::Type{Unit}) = 1
114+
108115
@testset "elementary functions" begin
109116
elementary_functions = [abs, abs2, sign,
110117
acos, acosh, asin, asinh, atan, atanh, sin, sinh,
111118
conj, cos, cosh, tan, tanh,
112119
exp, exp2, expm1, log, log10, log1p, log2,
113120
exponent, sqrt, gamma, lgamma,
114-
identity, zero,
121+
identity, zero, one, oneunit,
115122
iseven, isodd, ispow2,
116123
isfinite, isinf, isnan, iszero,
117124
isinteger, isreal, isempty, transpose, float]
@@ -121,9 +128,22 @@ end
121128
@test ismissing(f(missing))
122129
end
123130

124-
@test zero(Union{Int, Missing}) === 0
125-
@test zero(Union{Float64, Missing}) === 0.0
131+
for T in (Int, Float64)
132+
@test zero(Union{T, Missing}) === T(0)
133+
@test one(Union{T, Missing}) === T(1)
134+
@test oneunit(Union{T, Missing}) === T(1)
135+
end
136+
137+
for T in (Unit,)
138+
@test zero(Union{T, Missing}) === T(0)
139+
@test one(Union{T, Missing}) === 1
140+
@test oneunit(Union{T, Missing}) === T(1)
141+
end
142+
126143
@test_throws MethodError zero(Any)
144+
@test_throws MethodError one(Any)
145+
@test_throws MethodError oneunit(Any)
146+
127147
@test_throws MethodError zero(String)
128148
@test_throws MethodError zero(Union{String, Missing})
129149
end

0 commit comments

Comments
 (0)