@@ -19,35 +19,35 @@ showerror(io::IO, ex::MissingException) =
19
19
print (io, " MissingException: " , ex. msg)
20
20
21
21
nonmissingtype (:: Type{T} ) where {T} = Core. Compiler. typesubtract (T, Missing)
22
+ function nonmissingtype_checked (T:: Type )
23
+ R = nonmissingtype (T)
24
+ R >: T && error (" could not compute non-missing type" )
25
+ return R
26
+ end
22
27
23
- for U in (:Nothing , :Missing )
24
- @eval begin
25
- promote_rule (:: Type{$U} , :: Type{T} ) where {T} = Union{T, $ U}
26
- promote_rule (:: Type{Union{S,$U}} , :: Type{Any} ) where {S} = Any
27
- promote_rule (:: Type{Union{S,$U}} , :: Type{T} ) where {T,S} = Union{promote_type (T, S), $ U}
28
- promote_rule (:: Type{Any} , :: Type{$U} ) = Any
29
- promote_rule (:: Type{$U} , :: Type{Any} ) = Any
30
- # This definition is never actually used, but disambiguates the above definitions
31
- promote_rule (:: Type{$U} , :: Type{$U} ) = $ U
32
- end
28
+ promote_rule (T:: Type{Missing} , S:: Type ) = Union{S, Missing}
29
+ promote_rule (T:: Type{Union{Nothing, Missing}} , S:: Type ) = Union{S, Nothing, Missing}
30
+ function promote_rule (T:: Type{>:Union{Nothing, Missing}} , S:: Type )
31
+ R = nonnothingtype (T)
32
+ R >: T && return Any
33
+ T = R
34
+ R = nonmissingtype (T)
35
+ R >: T && return Any
36
+ T = R
37
+ R = promote_type (T, S)
38
+ return Union{R, Nothing, Missing}
33
39
end
34
- promote_rule (:: Type{Union{Nothing, Missing}} , :: Type{Any} ) = Any
35
- promote_rule (:: Type{Union{Nothing, Missing}} , :: Type{T} ) where {T} =
36
- Union{Nothing, Missing, T}
37
- promote_rule (:: Type{Union{Nothing, Missing, S}} , :: Type{Any} ) where {S} = Any
38
- promote_rule (:: Type{Union{Nothing, Missing, S}} , :: Type{T} ) where {T,S} =
39
- Union{Nothing, Missing, promote_type (T, S)}
40
-
41
- convert (:: Type{Union{T, Missing}} , x:: Union{T, Missing} ) where {T} = x
42
- convert (:: Type{Union{T, Missing}} , x) where {T} = convert (T, x)
43
- # To fix ambiguities
44
- convert (:: Type{Missing} , :: Missing ) = missing
45
- convert (:: Type{Union{Nothing, Missing}} , x:: Union{Nothing, Missing} ) = x
46
- convert (:: Type{Union{Nothing, Missing, T}} , x:: Union{Nothing, Missing, T} ) where {T} = x
47
- convert (:: Type{Union{Nothing, Missing}} , x) =
48
- throw (MethodError (convert, (Union{Nothing, Missing}, x)))
49
- # To print more appropriate message than "T not defined"
50
- convert (:: Type{Missing} , x) = throw (MethodError (convert, (Missing, x)))
40
+ function promote_rule (T:: Type{>:Missing} , S:: Type )
41
+ R = nonmissingtype (T)
42
+ R >: T && return Any
43
+ T = R
44
+ R = promote_type (T, S)
45
+ return Union{R, Missing}
46
+ end
47
+
48
+ convert (T:: Type{>:Union{Missing, Nothing}} , x) = convert (nonmissingtype_checked (nonnothingtype_checked (T)), x)
49
+ convert (T:: Type{>:Missing} , x) = convert (nonmissingtype_checked (T), x)
50
+
51
51
52
52
# Comparison operators
53
53
== (:: Missing , :: Missing ) = missing
@@ -108,10 +108,10 @@ round(::Missing, ::RoundingMode=RoundNearest; sigdigits::Integer=0, digits::Inte
108
108
round (:: Type{>:Missing} , :: Missing , :: RoundingMode = RoundNearest) = missing
109
109
round (:: Type{T} , :: Missing , :: RoundingMode = RoundNearest) where {T} =
110
110
throw (MissingException (" cannot convert a missing value to type $T : use Union{$T , Missing} instead" ))
111
- round (:: Type{T} , x:: Any , r:: RoundingMode = RoundNearest) where {T>: Missing } = round (nonmissingtype (T), x, r)
111
+ round (:: Type{T} , x:: Any , r:: RoundingMode = RoundNearest) where {T>: Missing } = round (nonmissingtype_checked (T), x, r)
112
112
# to fix ambiguities
113
- round (:: Type{T} , x:: Rational , r:: RoundingMode = RoundNearest) where {T>: Missing } = round (nonmissingtype (T), x, r)
114
- round (:: Type{T} , x:: Rational{Bool} , r:: RoundingMode = RoundNearest) where {T>: Missing } = round (nonmissingtype (T), x, r)
113
+ round (:: Type{T} , x:: Rational , r:: RoundingMode = RoundNearest) where {T>: Missing } = round (nonmissingtype_checked (T), x, r)
114
+ round (:: Type{T} , x:: Rational{Bool} , r:: RoundingMode = RoundNearest) where {T>: Missing } = round (nonmissingtype_checked (T), x, r)
115
115
116
116
# Handle ceil, floor, and trunc separately as they have no RoundingMode argument
117
117
for f in (:(ceil), :(floor), :(trunc))
@@ -120,9 +120,9 @@ for f in (:(ceil), :(floor), :(trunc))
120
120
($ f)(:: Type{>:Missing} , :: Missing ) = missing
121
121
($ f)(:: Type{T} , :: Missing ) where {T} =
122
122
throw (MissingException (" cannot convert a missing value to type $T : use Union{$T , Missing} instead" ))
123
- ($ f)(:: Type{T} , x:: Any ) where {T>: Missing } = $ f (nonmissingtype (T), x)
123
+ ($ f)(:: Type{T} , x:: Any ) where {T>: Missing } = $ f (nonmissingtype_checked (T), x)
124
124
# to fix ambiguities
125
- ($ f)(:: Type{T} , x:: Rational ) where {T>: Missing } = $ f (nonmissingtype (T), x)
125
+ ($ f)(:: Type{T} , x:: Rational ) where {T>: Missing } = $ f (nonmissingtype_checked (T), x)
126
126
end
127
127
end
128
128
0 commit comments