From 4e9bfa24fb89512377a2aff986d9bc1b90dbc91c Mon Sep 17 00:00:00 2001 From: Milan Bouchet-Valat Date: Sat, 13 Feb 2021 18:51:06 +0100 Subject: [PATCH 1/2] Implement float() and complex() for Missing `float` is already defined for `missing` and `AbstractArray{Union{T, Missing}}`. Add a definition taking a type consistent with these methods. Also implement similar definitions for `complex`. --- base/missing.jl | 9 ++++++++- test/missing.jl | 10 +++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/base/missing.jl b/base/missing.jl index ec92280b2d045..343e8014bbb0e 100644 --- a/base/missing.jl +++ b/base/missing.jl @@ -95,7 +95,7 @@ for f in (:(!), :(~), :(+), :(-), :(*), :(&), :(|), :(xor), :(zero), :(one), :(oneunit), :(isfinite), :(isinf), :(isodd), :(isinteger), :(isreal), :(isnan), - :(iszero), :(transpose), :(adjoint), :(float), :(conj), + :(iszero), :(transpose), :(adjoint), :(float), :(complex), :(conj), :(abs), :(abs2), :(iseven), :(ispow2), :(real), :(imag), :(sign), :(inv)) @eval ($f)(::Missing) = missing @@ -107,6 +107,13 @@ for f in (:(Base.zero), :(Base.one), :(Base.oneunit)) $f(T) end end +for f in (:(Base.float), :(Base.complex)) + @eval $f(::Type{Missing}) = Missing + @eval function $f(::Type{Union{T, Missing}}) where T + T === Any && throw(MethodError($f, (Any,))) # To prevent StackOverflowError + Union{$f(T), Missing} + end +end # Binary operators/functions for f in (:(+), :(-), :(*), :(/), :(^), :(mod), :(rem)) diff --git a/test/missing.jl b/test/missing.jl index 45cf3d0731e23..e49f46b8c8e0d 100644 --- a/test/missing.jl +++ b/test/missing.jl @@ -158,7 +158,7 @@ Base.one(::Type{Unit}) = 1 identity, zero, one, oneunit, iseven, isodd, ispow2, isfinite, isinf, isnan, iszero, - isinteger, isreal, transpose, adjoint, float, inv] + isinteger, isreal, transpose, adjoint, float, complex, inv] # All elementary functions return missing when evaluating missing for f in elementary_functions @@ -171,11 +171,15 @@ Base.one(::Type{Unit}) = 1 @test zero(Union{T, Missing}) === T(0) @test one(Union{T, Missing}) === T(1) @test oneunit(Union{T, Missing}) === T(1) + @test float(Union{T, Missing}) === Union{float(T), Missing} + @test complex(Union{T, Missing}) === Union{complex(T), Missing} end @test_throws MethodError zero(Union{Symbol, Missing}) @test_throws MethodError one(Union{Symbol, Missing}) @test_throws MethodError oneunit(Union{Symbol, Missing}) + @test_throws MethodError float(Union{Symbol, Missing}) + @test_throws MethodError complex(Union{Symbol, Missing}) for T in (Unit,) @test zero(Union{T, Missing}) === T(0) @@ -186,10 +190,14 @@ Base.one(::Type{Unit}) = 1 @test zero(Missing) === missing @test one(Missing) === missing @test oneunit(Missing) === missing + @test float(Missing) === Missing + @test complex(Missing) === Missing @test_throws MethodError zero(Any) @test_throws MethodError one(Any) @test_throws MethodError oneunit(Any) + @test_throws MethodError float(Any) + @test_throws MethodError complex(Any) @test_throws MethodError zero(String) @test_throws MethodError zero(Union{String, Missing}) From 351e82ea32c7d88308ae2f08916a69d2113a0d37 Mon Sep 17 00:00:00 2001 From: Milan Bouchet-Valat Date: Sat, 13 Feb 2021 22:18:33 +0100 Subject: [PATCH 2/2] Fix ambiguous test --- test/ambiguous.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/ambiguous.jl b/test/ambiguous.jl index 2c82906bc7ea5..25a3264489d6d 100644 --- a/test/ambiguous.jl +++ b/test/ambiguous.jl @@ -321,6 +321,8 @@ end pop!(need_to_handle_undef_sparam, which(Base._cat, Tuple{Any, AbstractArray})) pop!(need_to_handle_undef_sparam, which(Base.byteenv, (Union{AbstractArray{Pair{T,V}, 1}, Tuple{Vararg{Pair{T,V}}}} where {T<:AbstractString,V},))) pop!(need_to_handle_undef_sparam, which(Base.float, Tuple{AbstractArray{Union{Missing, T},N} where {T, N}})) + pop!(need_to_handle_undef_sparam, which(Base.float, Tuple{Type{Union{Missing, T}} where T})) + pop!(need_to_handle_undef_sparam, which(Base.complex, Tuple{Type{Union{Missing, T}} where T})) pop!(need_to_handle_undef_sparam, which(Base.zero, Tuple{Type{Union{Missing, T}} where T})) pop!(need_to_handle_undef_sparam, which(Base.one, Tuple{Type{Union{Missing, T}} where T})) pop!(need_to_handle_undef_sparam, which(Base.oneunit, Tuple{Type{Union{Missing, T}} where T}))