Skip to content

Commit

Permalink
Fix convert with Union{T, Nothing} (#213)
Browse files Browse the repository at this point in the history
  • Loading branch information
nalimilan authored Sep 27, 2019
1 parent 71e91da commit 13dfc1c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/value.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 10 additions & 0 deletions test/05_convert.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 13dfc1c

Please sign in to comment.