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> 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
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
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).