Skip to content

Commit 38a2627

Browse files
oxinaboxmkitti
authored andcommitted
Handle zero on arrays of unions of number types and missings (JuliaLang#53602)
1 parent 8516fce commit 38a2627

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

base/abstractarray.jl

+1
Original file line numberDiff line numberDiff line change
@@ -1218,6 +1218,7 @@ end
12181218
copymutable(itr) = collect(itr)
12191219

12201220
zero(x::AbstractArray{T}) where {T<:Number} = fill!(similar(x, typeof(zero(T))), zero(T))
1221+
zero(x::AbstractArray{S}) where {S<:Union{Missing, Number}} = fill!(similar(x, typeof(zero(S))), zero(S))
12211222
zero(x::AbstractArray) = map(zero, x)
12221223

12231224
## iteration support for arrays by iterating over `eachindex` in the array ##

test/abstractarray.jl

+10
Original file line numberDiff line numberDiff line change
@@ -1972,6 +1972,16 @@ end
19721972

19731973
@test zero([[2,2], [3,3,3]]) isa Vector{Vector{Int}}
19741974
@test zero([[2,2], [3,3,3]]) == [[0,0], [0, 0, 0]]
1975+
1976+
1977+
@test zero(Union{Float64, Missing}[missing]) == [0.0]
1978+
struct CustomNumber <: Number
1979+
val::Float64
1980+
end
1981+
Base.zero(::Type{CustomNumber}) = CustomNumber(0.0)
1982+
@test zero([CustomNumber(5.0)]) == [CustomNumber(0.0)]
1983+
@test zero(Union{CustomNumber, Missing}[missing]) == [CustomNumber(0.0)]
1984+
@test zero(Vector{Union{CustomNumber, Missing}}(undef, 1)) == [CustomNumber(0.0)]
19751985
end
19761986

19771987
@testset "`_prechecked_iterate` optimization" begin

0 commit comments

Comments
 (0)