# Inconsistency between zero(x) and zero(T::Type) for matrices

**URL:** <https://discourse.julialang.org/t/inconsistency-between-zero-x-and-zero-t-type-for-matrices/79117>\
**Category:** Internals & Design\
**Created:** [April 6, 2022, 6:23pm UTC](https://discourse.julialang.org/t/inconsistency-between-zero-x-and-zero-t-type-for-matrices/79117 "2022-04-06T18:23:57Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tbl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbl/32/5984_2.png) [@Tbl](https://discourse.julialang.org/u/Tbl)\
**Post date:** [April 6, 2022, 6:23pm UTC](https://discourse.julialang.org/t/inconsistency-between-zero-x-and-zero-t-type-for-matrices/79117/1 "2022-04-06T18:23:57Z")

</div>

I was experimenting with function `sum` and found out that it does not work for empty collections of matrices, Julia 1.7.2. The reason is there is no method `zero(::Type{Array{T,N}})`, thus you must specify `init` manually. However, there is a method `zero(::Array{T,N})`. As a consequence `zero([1 2; 3 4])` works but `zero(Matrix{Int})` does not work. Maybe there is a reason for this, but for me it seems to be very unintuitive. Similar thing happens for function `one` and consequently `prod`.

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [April 6, 2022, 6:28pm UTC](https://discourse.julialang.org/t/inconsistency-between-zero-x-and-zero-t-type-for-matrices/79117/2 "2022-04-06T18:28:25Z")

</div>

What would `zero(Matrix{Float64})` return? I guess you could have a lazy representation that doesn’t actually have a definite size, but that’s very different from what the other version does, be wise the input object has a size.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [April 6, 2022, 6:28pm UTC](https://discourse.julialang.org/t/inconsistency-between-zero-x-and-zero-t-type-for-matrices/79117/3 "2022-04-06T18:28:29Z")

</div>

> [@Tbl](#):
>
> zero(Matrix{Int})

But what should this call return?

Maybe `0*LinearAlgebra.I`? Be aware that this cannot be mutated/updated, though.

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [April 6, 2022, 6:31pm UTC](https://discourse.julialang.org/t/inconsistency-between-zero-x-and-zero-t-type-for-matrices/79117/4 "2022-04-06T18:31:55Z")

</div>

There is a family of initial-value issues

[https://github.com/JuliaLang/julia/issues/34003](https://github.com/JuliaLang/julia/issues/34003)

[https://github.com/JuliaLang/julia/issues/36103](https://github.com/JuliaLang/julia/issues/36103)

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [April 6, 2022, 6:47pm UTC](https://discourse.julialang.org/t/inconsistency-between-zero-x-and-zero-t-type-for-matrices/79117/5 "2022-04-06T18:47:46Z")

</div>

> [@Tbl](#):
>
> Maybe there is a reason for this, but for me it seems to be very unintuitive. Similar thing happens for function `one` and consequently `prod` .

The basic reason is that the size of the matrix is not encoded in the type. (If you use an `SMatrix` it works fine, in contrast, because [StaticArrays](https://github.com/JuliaArrays/StaticArrays.jl) include the size in the type.)

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [April 6, 2022, 8:24pm UTC](https://discourse.julialang.org/t/inconsistency-between-zero-x-and-zero-t-type-for-matrices/79117/6 "2022-04-06T20:24:59Z")

</div>

It’s also not of the type you asked for.

---

<div class="post-metadata">

**Author:** ![Tbl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbl/32/5984_2.png) [@Tbl](https://discourse.julialang.org/u/Tbl)\
**Post date:** [April 6, 2022, 8:58pm UTC](https://discourse.julialang.org/t/inconsistency-between-zero-x-and-zero-t-type-for-matrices/79117/7 "2022-04-06T20:58:57Z")

</div>

A, yes, this is unfortunate. It skipped my mind this is the case. I guess there is no good fix then. Functions `sum` and `prod` becoming type unstable would be even worse.
