From d183b43db0577964a08e877e5d91da8873a6776b Mon Sep 17 00:00:00 2001 From: Frames White Date: Tue, 5 Mar 2024 15:29:17 +0800 Subject: [PATCH 1/3] Handle zero on arrays of unions of custom number types and missings --- base/abstractarray.jl | 1 + test/abstractarray.jl | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/base/abstractarray.jl b/base/abstractarray.jl index a9431197932dd..64c16572be1ae 100644 --- a/base/abstractarray.jl +++ b/base/abstractarray.jl @@ -1218,6 +1218,7 @@ end copymutable(itr) = collect(itr) zero(x::AbstractArray{T}) where {T<:Number} = fill!(similar(x, typeof(zero(T))), zero(T)) +zero(x::AbstractArray{S}) where {T<:Number, S<:Union{Missing, T}} = fill!(similar(x, typeof(zero(S))), zero(S)) zero(x::AbstractArray) = map(zero, x) ## iteration support for arrays by iterating over `eachindex` in the array ## diff --git a/test/abstractarray.jl b/test/abstractarray.jl index 9d9c9b9c87776..03cd83e0307e3 100644 --- a/test/abstractarray.jl +++ b/test/abstractarray.jl @@ -1972,6 +1972,15 @@ end @test zero([[2,2], [3,3,3]]) isa Vector{Vector{Int}} @test zero([[2,2], [3,3,3]]) == [[0,0], [0, 0, 0]] + + + @test zero(Union{Float64, Missing}[missing]) == [0.0] + struct CustomNumber <: Number + val::Float64 + end + Base.zero(::Type{CustomNumber}) = CustomNumber(0.0) + @test zero([CustomNumber(5.0)]) == [CustomNumber(0.0)] + @test zero(Union{CustomNumber, Missing}[missing]) == [CustomNumber(0.0)] end @testset "`_prechecked_iterate` optimization" begin From fc1aab204d85ccec480bcd73ed5a86c054aa79c9 Mon Sep 17 00:00:00 2001 From: Frames White Date: Tue, 5 Mar 2024 15:47:29 +0800 Subject: [PATCH 2/3] test also the undef case --- test/abstractarray.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/test/abstractarray.jl b/test/abstractarray.jl index 03cd83e0307e3..1411864acfa03 100644 --- a/test/abstractarray.jl +++ b/test/abstractarray.jl @@ -1981,6 +1981,7 @@ end Base.zero(::Type{CustomNumber}) = CustomNumber(0.0) @test zero([CustomNumber(5.0)]) == [CustomNumber(0.0)] @test zero(Union{CustomNumber, Missing}[missing]) == [CustomNumber(0.0)] + @test zero(Vector{Union{CustomNumber, Missing}}(undef, 1)) == [CustomNumber(0.0)] end @testset "`_prechecked_iterate` optimization" begin From 0fe4977b3c32a62447f16b52216d6aafe6284bd2 Mon Sep 17 00:00:00 2001 From: Jameson Nash Date: Fri, 8 Mar 2024 16:21:31 -0500 Subject: [PATCH 3/3] Update base/abstractarray.jl Co-authored-by: Jeff Bezanson --- base/abstractarray.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/abstractarray.jl b/base/abstractarray.jl index 64c16572be1ae..9042e5f81e462 100644 --- a/base/abstractarray.jl +++ b/base/abstractarray.jl @@ -1218,7 +1218,7 @@ end copymutable(itr) = collect(itr) zero(x::AbstractArray{T}) where {T<:Number} = fill!(similar(x, typeof(zero(T))), zero(T)) -zero(x::AbstractArray{S}) where {T<:Number, S<:Union{Missing, T}} = fill!(similar(x, typeof(zero(S))), zero(S)) +zero(x::AbstractArray{S}) where {S<:Union{Missing, Number}} = fill!(similar(x, typeof(zero(S))), zero(S)) zero(x::AbstractArray) = map(zero, x) ## iteration support for arrays by iterating over `eachindex` in the array ##