Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix convert with Union{T, Nothing} #213

Merged
merged 1 commit into from
Sep 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these two definitions for Nothing and Missing needed? It seems that by leaving https://github.com/JuliaData/CategoricalArrays.jl/pull/213/files#diff-0c97238b80ec111debf6823967ed4c9eR87 only (ie. the default convert) we should get exactly the same?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's due to ambiguities with methods defined in Base.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, this is the pain I have with DataFrames.jl every time I do a major redesign of something that touches Base.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hopefully with #201 we will be able to simplify this.

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