Options
The option
type is a parametric, predefined variant type that is
used to express whether there is a value of some type or none. This is
especially useful when calling a partial function, that is, a
function that is not defined for some inputs. In that case, the value
of the option
type would be None()
, otherwise Some(v)
, where v
is some meaningful value of any type. A typical example from
arithmetics is the division:
Note: See the predefined namespace Option
Euclidean Division
For cases when you need both the quotient and the remainder, LIGO
provides the ediv
operation. ediv(x,y)
returns Some (quotient,
remainder)
, unless y
is zero, in which case it returns None
. The
function ediv
is overloaded to accept all the combinations (4) of
natural and integer numbers:
Checking positivity
You can check if a value is a natural number (nat
) by using a
predefined cast function which accepts an integer (int
) and returns
an optional natural number (nat
): if the result is None
, then the
given integer was positive, otherwise the corresponding natural number
n
is given with Some(n)
.