From 38f6d606c30220b3fbd334af7849ef002fdb7a66 Mon Sep 17 00:00:00 2001 From: Milan Bouchet-Valat Date: Fri, 27 Sep 2019 10:08:28 +0200 Subject: [PATCH] Fix convert with Union{T, Nothing} --- src/value.jl | 4 ++-- test/05_convert.jl | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/value.jl b/src/value.jl index 2b285670..346bb267 100644 --- a/src/value.jl +++ b/src/value.jl @@ -87,9 +87,9 @@ Base.convert(::Type{Union{T, Nothing}}, x::T) where {T <: CatValue} = x Base.convert(::Type{S}, x::T) where {S, T <: CatValue} = T <: S ? x : convert(S, get(x)) Base.convert(::Type{Union{S, Missing}}, x::T) where {S, T <: CatValue} = - T <: S ? x : convert(S, get(x)) + T <: Union{S, Missing} ? x : convert(Union{S, Missing}, get(x)) Base.convert(::Type{Union{S, Nothing}}, x::T) where {S, T <: CatValue} = - T <: S ? x : convert(S, get(x)) + T <: Union{S, Nothing} ? x : convert(Union{S, Nothing}, get(x)) (::Type{T})(x::T) where {T <: CatValue} = x diff --git a/test/05_convert.jl b/test/05_convert.jl index 76ba091c..c9926c5d 100644 --- a/test/05_convert.jl +++ b/test/05_convert.jl @@ -179,4 +179,14 @@ end @test convert(CategoricalPool{Float64, UInt8}, pool).ordered === true end +@testset "convert() with Union{T, Nothing}" begin + pool = CategoricalPool([nothing, 2, 3]) + v1 = catvalue(1, pool) + v2 = catvalue(2, pool) + @test convert(Union{Int, Nothing}, v1) === nothing + @test convert(Union{Int, Nothing}, v2) === 2 + @test convert(Union{Float64, Nothing}, v2) === 2.0 +end + + end