From 8384dab0d84f83bea00a989d630d836590557eba Mon Sep 17 00:00:00 2001 From: Frames White Date: Tue, 26 Sep 2023 18:20:46 +0800 Subject: [PATCH 1/4] Make zero on array of arrays apply recursively --- NEWS.md | 1 + base/abstractarray.jl | 3 ++- test/abstractarray.jl | 11 +++++++++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 824d730ed3c3c..baa1dc704c0f9 100644 --- a/NEWS.md +++ b/NEWS.md @@ -35,6 +35,7 @@ New library features -------------------- * `replace(string, pattern...)` now supports an optional `IO` argument to write the output to a stream rather than returning a string ([#48625]). +* `zero(::AbstractArray)` now applies recursively, so `zero([[1,2],[3,4,5]])` now produces `[[0,0],[0,0,0]]` rather than erroring [#38064]. Standard library changes ------------------------ diff --git a/base/abstractarray.jl b/base/abstractarray.jl index e74f031b77451..9a842929edcd5 100644 --- a/base/abstractarray.jl +++ b/base/abstractarray.jl @@ -1198,7 +1198,8 @@ function copymutable(a::AbstractArray) end copymutable(itr) = collect(itr) -zero(x::AbstractArray{T}) where {T} = fill!(similar(x, typeof(zero(T))), zero(T)) +zero(x::AbstractArray{T}) where {T<:Number} = fill!(similar(x, typeof(zero(T))), zero(T)) +zero(x::AbstractArray) = map(zero, x) ## iteration support for arrays by iterating over `eachindex` in the array ## # Allows fast iteration by default for both IndexLinear and IndexCartesian arrays diff --git a/test/abstractarray.jl b/test/abstractarray.jl index 29bc514bb1802..08fe0eeee036a 100644 --- a/test/abstractarray.jl +++ b/test/abstractarray.jl @@ -1860,3 +1860,14 @@ f45952(x) = [x;;] @test_throws "invalid index: true of type Bool" isassigned(A, 1, true) @test_throws "invalid index: true of type Bool" isassigned(A, true) end + +@testset "zero" begin + @test zero([1 2; 3 4]) isa Matrix{Int} + @test zero([1 2; 3 4]) == [0 0; 0 0] + + @test zero([1.0]) isa Vector{Float64} + @test zero([1.0]) == [0.0] + + @test zero([[2,2], [3,3,3]]) isa Vector{Vector{Int}} + @test zero([[2,2], [3,3,3]]) == [[0,0], [0, 0, 0]] +end \ No newline at end of file From fb235de0022ac225807db1d9a9cfba4472f85e1f Mon Sep 17 00:00:00 2001 From: Frames White Date: Wed, 27 Sep 2023 15:23:20 +0800 Subject: [PATCH 2/4] add trailing newline --- base/abstractarray.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/base/abstractarray.jl b/base/abstractarray.jl index 9a842929edcd5..dff565c9f8293 100644 --- a/base/abstractarray.jl +++ b/base/abstractarray.jl @@ -3541,3 +3541,4 @@ function circshift!(a::AbstractVector, shift::Integer) reverse!(a) return a end + From 49981d460d2a9dc12738dc5b519ea2d070496370 Mon Sep 17 00:00:00 2001 From: Frames White Date: Wed, 27 Sep 2023 16:49:32 +0800 Subject: [PATCH 3/4] Fix whitespace --- base/abstractarray.jl | 1 - test/abstractarray.jl | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/base/abstractarray.jl b/base/abstractarray.jl index dff565c9f8293..9a842929edcd5 100644 --- a/base/abstractarray.jl +++ b/base/abstractarray.jl @@ -3541,4 +3541,3 @@ function circshift!(a::AbstractVector, shift::Integer) reverse!(a) return a end - diff --git a/test/abstractarray.jl b/test/abstractarray.jl index 08fe0eeee036a..5c81131ffdbee 100644 --- a/test/abstractarray.jl +++ b/test/abstractarray.jl @@ -1870,4 +1870,4 @@ end @test zero([[2,2], [3,3,3]]) isa Vector{Vector{Int}} @test zero([[2,2], [3,3,3]]) == [[0,0], [0, 0, 0]] -end \ No newline at end of file +end From 958bfbe1bc8e5ded184a663f78afa8dace440101 Mon Sep 17 00:00:00 2001 From: inky Date: Sat, 10 Feb 2024 22:08:01 -0600 Subject: [PATCH 4/4] Update test/abstractarray.jl --- test/abstractarray.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/abstractarray.jl b/test/abstractarray.jl index 9cf3fdb533262..c3409a4a059fb 100644 --- a/test/abstractarray.jl +++ b/test/abstractarray.jl @@ -1960,4 +1960,4 @@ end @test zero([[2,2], [3,3,3]]) isa Vector{Vector{Int}} @test zero([[2,2], [3,3,3]]) == [[0,0], [0, 0, 0]] -end \ No newline at end of file +end