ZeroRing.jl

A number type that has only a single value (zero) and needs no storage
Author eschnett
Popularity
1 Star
Updated Last
2 Years Ago
Started In
August 2020

The Zero ring

A number type that has only one value (zero) and needs no storage: see https://en.wikipedia.org/wiki/Zero_ring.

  • GitHub: Source code repository
  • GitHub CI

Why would I need this?

The zero ring is a very special type of number: There is only one value (zero). Booleans, for example, have two values (false and true), Int8 have 256 values, etc. A ZeroElem has only one value (zeroelem). This is similar to Nothing, except that ZeroElem is a subtype of Number whereas Nothing isn't, and it doesn't indicate that some information is missing (as Missing would).

At times, you might have a data structure holding numbers, but one is only interested in the "skeleton" of the data structure, and not into the numbers it can hold. Examples are:

  • a graph with weighted edges, but the weights are not relevant
  • a sparse matrix, but only the sparsity structure is interesting

Why can't I use Nothing or Missing instead?

Nothing is not a subtype of Number, which makes some linear algebra operations fail. It would, of course, in principle be possible to make Nothing be a subtype of Number, but this is not a good idea as Nothing is a general concept that has nothing to do with numbers, addition, multiplication, etc.

In a similar manner, Missing indicates that certain information is missing. This is not the case here; all the necessary information is there. It makes sense to define missing + zeroelem, and the result should be missing.

What is the advantage of ZeroElem?

A ZeroElem number takes no storage:

julia> @allocated Array{ZeroElem}(undef, 1000)
80

julia> @allocated Array{Nothing}(undef, 1000)
80

julia> @allocated Array{Bool}(undef, 1000)
1088

The 80 bytes reported here are for the array metadata (its size and shape etc.); there are no actual data allocated.