# Iszero and zero: wrong usage (?) and bad error message

**URL:** <https://discourse.julialang.org/t/iszero-and-zero-wrong-usage-and-bad-error-message/71907>\
**Category:** General Usage\
**Created:** [November 22, 2021, 6:55pm UTC](https://discourse.julialang.org/t/iszero-and-zero-wrong-usage-and-bad-error-message/71907 "2021-11-22T18:55:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [November 22, 2021, 6:55pm UTC](https://discourse.julialang.org/t/iszero-and-zero-wrong-usage-and-bad-error-message/71907/1 "2021-11-22T18:55:10Z")

</div>

I am trying to use `iszero` for a custom struct. I defined `zero` for it, which appeared to be the necessary method. But `iszero` does not work, and the error message is not clear, because it complains that the `zero` function (just defined) does not exist.

```julia
julia> struct S
           x::Int
       end

julia> Base.zero(::Type{S}) = S(0)

julia> zero(S)
S(0)

julia> x = S(0)
S(0)

julia> iszero(x)
ERROR: MethodError: no method matching zero(::S)
Closest candidates are:
  zero(::Union{Type{P}, P}) where P<:Dates.Period at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/Dates/src/periods.jl:53
  zero(::LinearAlgebra.UniformScaling{T}) where T at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/LinearAlgebra/src/uniformscaling.jl:136
  zero(::SparseArrays.AbstractSparseArray) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/SparseArrays/src/SparseArrays.jl:55
  ...
Stacktrace:
 [1] iszero(x::S)
   @ Base ./number.jl:40
 [2] top-level scope
   @ REPL[12]:1

julia> 

```

If I define

```julia
Base.zero(x::S) = S(0)

```

the problem is solved. However, this seems redundant with the definition of `Base.zero(::Type{S})` , since (I guess) there could be a

```julia
Base.zero(x::T) where T = Base.zero(T)

```

in fact I am surprised that this is not already implemented (and curious for the reason).

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [November 22, 2021, 7:17pm UTC](https://discourse.julialang.org/t/iszero-and-zero-wrong-usage-and-bad-error-message/71907/2 "2021-11-22T19:17:18Z")

</div>

What if not all information is available in the type parameters?

```julia
julia> struct A{T}
           v1::T
           v2::Number
       end

julia> Base.zero(a::A) = A(zero(a.v1), zero(a.v2))

julia> zero(A(0.3, 1))
A{Float64}(0.0, 0)

julia> Base.zero(::Type{A}) where T = A(0, 0)

julia> zero(A)
A{Int64}(0, 0)

```

---

<div class="post-metadata">

**Author:** ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)\
**Post date:** [November 22, 2021, 7:30pm UTC](https://discourse.julialang.org/t/iszero-and-zero-wrong-usage-and-bad-error-message/71907/3 "2021-11-22T19:30:29Z")

</div>

Another example is vectors/arrays: only the dimension and the element type are available in the type itself, not the size (which is nevertheless needed to build a neutral element of the correct size).

i.e:

```julia
julia> v = [1,2,3]
3-element Vector{Int64}:
 1
 2
 3

julia> zero(v) # needs the size of v, which is not contained in its type
3-element Vector{Int64}:
 0
 0
 0

```
