# Zero() and iszero() for mutable structs

**URL:** <https://discourse.julialang.org/t/zero-and-iszero-for-mutable-structs/46841>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [September 18, 2020, 1:01pm UTC](https://discourse.julialang.org/t/zero-and-iszero-for-mutable-structs/46841 "2020-09-18T13:01:28Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![efokken](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@efokken](https://discourse.julialang.org/u/efokken)\
**Post date:** [September 18, 2020, 1:01pm UTC](https://discourse.julialang.org/t/zero-and-iszero-for-mutable-structs/46841/1 "2020-09-18T13:01:28Z")

</div>

Hi there!  
It is useful for me to store a `mutable struct Line` in a sparse matrix.  
I had the impression that it is useful to define  
`Base.zero(::Line) = Line(0,1,0)`  
and  
`Base.zero(::Type{Line}) = Line(0,1,0)`

unfortunately the following shows up in Julia:  
`iszero(zero(Line))`

`false`  
Also:  
`zero(Line)==zero(Line)`

`false`

The same instead works as expected for usual static structs but I am not sure whether I can work with static structs.  
I think I understand why this is (as for static structs there is one and only one zero struct, but for mutables, there may be copies that later on become actually different).  
Still, is there another way to make this work?

---

<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:** [September 18, 2020, 1:48pm UTC](https://discourse.julialang.org/t/zero-and-iszero-for-mutable-structs/46841/2 "2020-09-18T13:48:31Z")

</div>

`iszero(x::Line)` compares `x == zero(::Line)` with `==` defaults to `===`, so really you need to define `==` to actually compare fields value

---

<div class="post-metadata">

**Author:** ![efokken](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@efokken](https://discourse.julialang.org/u/efokken)\
**Post date:** [September 18, 2020, 1:54pm UTC](https://discourse.julialang.org/t/zero-and-iszero-for-mutable-structs/46841/3 "2020-09-18T13:54:33Z")

</div>

Oh, that makes sense, thank you!

Now that I think about it, would that be a good default behaviour for iszero for mutable structs?  
I mean, when I have defined a zero for these, I guess I always want to know, whether a given instance currently behaves like a zero, whatever that might mean for the structure.  
Then again I guess the use-case is so rare, nobody really cares.

---

<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:** [September 18, 2020, 4:15pm UTC](https://discourse.julialang.org/t/zero-and-iszero-for-mutable-structs/46841/4 "2020-09-18T16:15:15Z")

</div>

> [@efokken](#):
>
> I had the impression that it is useful to define  
> `Base.zero(::Line)`

In general, you should define `zero` for types that have a `+` operation, in which case `zero` should return the additive identity. If you aren’t doing arithmetic on your type (no `+`), then you probably shouldn’t define `zero` (regardless of whether your data is stored in a matrix).

---

<div class="post-metadata">

**Author:** ![efokken](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@efokken](https://discourse.julialang.org/u/efokken)\
**Post date:** [September 18, 2020, 4:30pm UTC](https://discourse.julialang.org/t/zero-and-iszero-for-mutable-structs/46841/5 "2020-09-18T16:30:18Z")

</div>

I wanted to have it in order to access non-declared elements of the sparse matrix.  
Is there some other fast way to check, whether a given index-pair of a sparse matrix is filled?

---

<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:** [September 19, 2020, 4:34pm UTC](https://discourse.julialang.org/t/zero-and-iszero-for-mutable-structs/46841/6 "2020-09-19T16:34:43Z")

</div>

> [@efokken](#):
>
> Is there some other fast way to check, whether a given index-pair of a sparse matrix is filled?

In Julia 1.6 there is a `Base.isstored(A, i,j)` function (added by [#33821](https://github.com/JuliaLang/julia/pull/33821)), though it is currently undocumented. The [implementation is pretty trivial](https://github.com/JuliaLang/julia/blob/30af85a664127323a43ba181956d16222392d38b/stdlib/SparseArrays/src/sparsematrix.jl#L203-L210), though, and uses only the documented `rowvals` and `nzrange` functions if you want to copy it.

---

<div class="post-metadata">

**Author:** ![efokken](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@efokken](https://discourse.julialang.org/u/efokken)\
**Post date:** [September 22, 2020, 12:16pm UTC](https://discourse.julialang.org/t/zero-and-iszero-for-mutable-structs/46841/7 "2020-09-22T12:16:45Z")

</div>

Sounds reasonable, thanks for the hint, I will do that with Julia 1.6+.  
But for now I’ll stick with the zero solution, if I actually need it, not sure yet.

For the record: My setting is a sparse graph, whose line-structs I store in a sparse matrix.
