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

WIP/RFC: basic promote_rule #42

Closed
wants to merge 1 commit into from
Closed
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
10 changes: 9 additions & 1 deletion src/array.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## Common code for CategoricalArray and NullableCategoricalArray

import Base: convert, copy, copy!, getindex, setindex!, similar, size,
linearindexing, unique, vcat
linearindexing, unique, vcat, promote_rule

# Used for keyword argument default value
_isordered(x::AbstractCategoricalArray) = isordered(x)
Expand Down Expand Up @@ -339,6 +339,14 @@ end
@inbounds A.refs[I...] = get!(A.pool, convert(T, v))
end

@inline function promote_rule{T,N,R,T2}(::Type{CatArray{T,N,R}}, ::Type{Array{T2}})
Copy link
Member

Choose a reason for hiding this comment

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

You don'tneed @inline. Also, you should add a type parameter <:CatArray and promote to it instead of CatArray (which is a union type). Instead of T and T2, the rest of the code base uses S and T.

CatArray{promote_type(T, T2), N, R}
end

@inline function promote_rule{T,N,R,T2<:Nullable}(::Type{CatArray{T,N,R}}, ::Type{Array{T2}})
NullableCategoricalArray{promote_type(T, eltype(T2)), N, R}
end

function mergelevels(ordered, levels...)
T = Base.promote_eltype(levels...)
res = Array{T}(0)
Expand Down
3 changes: 3 additions & 0 deletions test/13_arraycommon.jl
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,9 @@ for (CA, A) in ((CategoricalArray, Array), (NullableCategoricalArray, NullableAr
@test x == ux
@test typeof(x) == CA{Int, 1, UInt8}
@test typeof(ux) == CA{Int, 1, CategoricalArrays.DefaultRefType}

@test promote_type(typeof(CA([3,2,1])), typeof([1,2])) == CA{Int,1,DefaultRefType}
Copy link
Member

Choose a reason for hiding this comment

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

Use the types directly instead of creating the objects and calling typeof.

@test promote_type(typeof(CA([3,2,1])), typeof(NullableArray([1,3]))) == NullableCategoricalArray{Int,1,DefaultRefType}
end
Copy link
Member

Choose a reason for hiding this comment

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

Could you also test cases in which the element types are different, e.g. Int and Float64? Would be good to test promoting (Nullable)CategoricalArray with CA, and check in particular that the reference type is as expected.


end