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

When Base has the unwrap function, we can avoid defining generic fall… #201

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
51 changes: 21 additions & 30 deletions src/value.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,48 +80,39 @@ Base.promote_rule(::Type{C1}, ::Type{C2}) where {C1<:CatValue, C2<:CatValue} =

Base.convert(::Type{Ref}, x::CatValue) = RefValue{leveltype(x)}(x)
Base.convert(::Type{String}, x::CatValue) = convert(String, get(x))
Base.convert(::Type{Any}, x::CatValue) = x

# Defined separately to avoid ambiguities
Base.convert(::Type{AbstractString}, x::CategoricalString) = x
Base.convert(::Type{T}, x::T) where {T <: CatValue} = x
Base.convert(::Type{Union{T, Missing}}, x::T) where {T <: CatValue} = x
Base.convert(::Type{Union{T, Nothing}}, x::T) where {T <: CatValue} = x

@static if isdefined(Base, :unwrap)
Copy link
Member

Choose a reason for hiding this comment

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

Please switch to a VERSION check once the PR is merged in Julia, so that Femtocleaner automatically removes the code the day we will require Julia 1.3.

Base.unwrap(x::CatValue) = get(x)
else
# General fallbacks
Base.convert(::Type{S}, x::T) where {S, T <: CatValue} =
T <: S ? x : convert(S, 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.

Just to be sure, do things like convert(CategoricalValue, x::CategoricalValue{String,UInt32}) work with this PR? That was handled by the T<:S branch.

Base.convert(::Type{Union{S, Missing}}, x::T) where {S, T <: CatValue} =
T <: S ? x : convert(S, get(x))
Base.convert(::Type{Union{S, Nothing}}, x::T) where {S, T <: CatValue} =
T <: S ? x : convert(S, get(x))
Base.convert(::Type{Any}, x::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))
Base.convert(::Type{Union{S, Nothing}}, x::T) where {S, T <: CatValue} =
T <: S ? x : convert(S, get(x))
end

(::Type{T})(x::T) where {T <: CatValue} = x

Base.Broadcast.broadcastable(x::CatValue) = Ref(x)

if VERSION >= v"0.7.0-DEV.2797"
function Base.show(io::IO, x::CatValue)
if Missings.T(get(io, :typeinfo, Any)) === Missings.T(typeof(x))
print(io, repr(x))
elseif isordered(pool(x))
@printf(io, "%s %s (%i/%i)",
typeof(x), repr(x),
order(x), length(pool(x)))
else
@printf(io, "%s %s", typeof(x), repr(x))
end
end
else
function Base.show(io::IO, x::CatValue)
if get(io, :compact, false)
print(io, repr(x))
elseif isordered(pool(x))
@printf(io, "%s %s (%i/%i)",
typeof(x), repr(x),
order(x), length(pool(x)))
else
@printf(io, "%s %s", typeof(x), repr(x))
end
function Base.show(io::IO, x::CatValue)
if Missings.T(get(io, :typeinfo, Any)) === Missings.T(typeof(x))
print(io, repr(x))
elseif isordered(pool(x))
@printf(io, "%s %s (%i/%i)",
typeof(x), repr(x),
order(x), length(pool(x)))
else
@printf(io, "%s %s", typeof(x), repr(x))
end
end

Expand Down