diff --git a/src/nullablevector.jl b/src/nullablevector.jl index cd48417..2eff274 100644 --- a/src/nullablevector.jl +++ b/src/nullablevector.jl @@ -1,3 +1,5 @@ +import Base: vcat, hcat, typed_vcat, typed_hcat, promote_eltype + """ push!{T, V}(X::NullableVector{T}, v::V) @@ -311,3 +313,20 @@ function Base.empty!(X::NullableVector) empty!(X.isnull) return X end + +function vcat(A::AbstractVector, B::NullableVector) + typed_vcat(promote_eltype(A, B), NullableArray(A), B) +end + +function vcat(A::AbstractMatrix, B::NullableMatrix) + typed_vcat(promote_eltype(A, B), NullableArray(A), B) +end + +function vcat(A::AbstractArray, B::NullableArray) + typed_vcat(promote_eltype(A, B), NullableArray(A), B) +end + +NullableVecOrMat = Union{NullableVector, NullableMatrix} +function hcat(A::AbstractVecOrMat, B::NullableVecOrMat) + typed_hcat(promote_eltype(A, B), NullableArray(A), B) +end diff --git a/test/nullablevector.jl b/test/nullablevector.jl index 7bdc512..dd19dd2 100644 --- a/test/nullablevector.jl +++ b/test/nullablevector.jl @@ -223,4 +223,30 @@ module TestNullableVector X = NullableArray(A, M) empty!(X) @test isempty(X) + + @test typeof(vcat(NullableArray(1:2), 3:4)) == NullableArray{Int,1} + @test typeof(vcat(1:2, NullableArray(3:4))) == NullableArray{Int,1} + @test typeof(vcat(NullableArray([1 2]), [3 4])) == NullableArray{Int,2} + @test typeof(vcat([1 2], NullableArray([3 4]))) == NullableArray{Int,2} + + @test typeof(hcat(NullableArray(1:2), 3:4)) == NullableArray{Int,2} + @test typeof(hcat(1:2, NullableArray(3:4))) == NullableArray{Int,2} + @test typeof(hcat(NullableArray([1 2]), [3 4])) == NullableArray{Int,2} + @test typeof(hcat([1 2], NullableArray([3 4]))) == NullableArray{Int,2} + + # add these back if we can find a general solution to propogating array type + # see https://github.com/JuliaLang/julia/issues/2326 + # @test typeof(vcat(NullableArray(1:2), 3:4, 5:6)) == NullableArray{Int,1} + # @test typeof(vcat(1:2, NullableArray(3:4), 5:6)) == NullableArray{Int,1} + # @test typeof(vcat(1:2, 3:4, NullableArray(5:6))) == NullableArray{Int,1} + # @test typeof(vcat(NullableArray([1 2]), [3 4], [5 6])) == NullableArray{Int,2} + # @test typeof(vcat([1 2], NullableArray([3 4]), [5 6])) == NullableArray{Int,2} + # @test typeof(vcat([1 2], [3 4], NullableArray([5 6]))) == NullableArray{Int,2} + + # @test typeof(hcat(NullableArray(1:2), 3:4, 5:6)) == NullableArray{Int,2} + # @test typeof(hcat(1:2, NullableArray(3:4), 5:6)) == NullableArray{Int,2} + # @test typeof(hcat(1:2, 3:4, NullableArray(5:6))) == NullableArray{Int,2} + # @test typeof(hcat(NullableArray([1 2]), [3 4], [5 6])) == NullableArray{Int,2} + # @test typeof(hcat([1 2], NullableArray([3 4]), [5 6])) == NullableArray{Int,2} + # @test typeof(hcat([1 2], [3 4], NullableArray([5 6]))) == NullableArray{Int,2} end