From cb7c4005303a5be0b17607a8a130371989b98d8e Mon Sep 17 00:00:00 2001 From: Jiahao Chen Date: Tue, 21 Oct 2014 20:33:07 -0400 Subject: [PATCH] Add support for zero(::Array{Array}) The problem with the existing definition is that it tries to call zero(::Type{Array}), which fails because it doesn't know what size the eltype should be. The solution here is simple - if the outer Array is not empty, call zero on its first element. A similar problem occurs with one() but that is harder to fix. --- base/abstractarray.jl | 2 +- test/arrayops.jl | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/base/abstractarray.jl b/base/abstractarray.jl index 61ead2f0bc98c..31e8396e7ac8d 100644 --- a/base/abstractarray.jl +++ b/base/abstractarray.jl @@ -237,7 +237,7 @@ function copy_transpose!{R,S}(B::AbstractVecOrMat{R}, ir_dest::Range{Int}, jr_de return B end -zero{T}(x::AbstractArray{T}) = fill!(similar(x), zero(T)) +zero{T}(x::AbstractArray{T}) = fill!(similar(x), zero(isempty(x) ? T : x[1])) ## iteration support for arrays as ranges ## diff --git a/test/arrayops.jl b/test/arrayops.jl index 5b8cb62b4a822..11850437a5c36 100644 --- a/test/arrayops.jl +++ b/test/arrayops.jl @@ -910,3 +910,8 @@ function pr8622() return indexin(x,y) end @test pr8622() == [0,3,1,0] + +#Array of Array tests +let A = [zeros(2,2) for i=1:2, j=1:2] + @test zero(A) == A +end