Skip to content

Commit

Permalink
fix eltype of zero(::AbstractVector) (#41380)
Browse files Browse the repository at this point in the history
  • Loading branch information
jishnub authored Jun 30, 2021
1 parent e47ee99 commit b617e8d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1133,7 +1133,7 @@ function copymutable(a::AbstractArray)
end
copymutable(itr) = collect(itr)

zero(x::AbstractArray{T}) where {T} = fill!(similar(x), zero(T))
zero(x::AbstractArray{T}) where {T} = fill!(similar(x, typeof(zero(T))), zero(T))

## iteration support for arrays by iterating over `eachindex` in the array ##
# Allows fast iteration by default for both IndexLinear and IndexCartesian arrays
Expand Down
10 changes: 10 additions & 0 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2908,3 +2908,13 @@ end
@test [fill(1); fill(2, (2,1,1))] == reshape([1; 2; 2], (3, 1, 1))
@test_throws DimensionMismatch [fill(1); rand(2, 2, 2)]
end

@testset "eltype of zero for arrays (issue #41348)" begin
for a in Any[[DateTime(2020), DateTime(2021)], [Date(2000), Date(2001)], [Time(1), Time(2)]]
@test a + zero(a) == a
b = reshape(a, :, 1)
@test b + zero(b) == b
c = view(b, 1:1, 1:1)
@test c + zero(c) == c
end
end

0 comments on commit b617e8d

Please sign in to comment.