From 8a0996dacd89169668227f8668d8b8ba61c2f3a3 Mon Sep 17 00:00:00 2001 From: Johan Gustafsson Date: Tue, 15 Nov 2016 22:03:48 +0100 Subject: [PATCH] basic promote_rule --- src/array.jl | 10 +++++++++- test/13_arraycommon.jl | 3 +++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/array.jl b/src/array.jl index e3a32aea..7450822b 100644 --- a/src/array.jl +++ b/src/array.jl @@ -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) @@ -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}}) + 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) diff --git a/test/13_arraycommon.jl b/test/13_arraycommon.jl index 9fe0a67b..e65deda9 100644 --- a/test/13_arraycommon.jl +++ b/test/13_arraycommon.jl @@ -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} + @test promote_type(typeof(CA([3,2,1])), typeof(NullableArray([1,3]))) == NullableCategoricalArray{Int,1,DefaultRefType} end end