Optionals are a pervasive programing pattern in OCaml. Since Michelson and LIGO are both inspired by OCaml, optional types are available in LIGO as well. Similarly, OCaml features a unit type, and LIGO features it as well. Both the option type and the unit types are instances of a more general kind of types: variant types (sometimes called sum types).
unit type in Michelson or LIGO is a predefined type that
contains only one value that carries no information. It is used when
no relevant information is required or produced. Here is how it used.
In PascaLIGO, the unique value of the
unit type is
Here is how we define a coin as being either head or tail (and nothing else):
Tail in the definition of the type
called data constructors, or variants. In this particular, they
carry no information beyond their names, so they are called constant
In general, it is interesting for variants to carry some information, and thus go beyond enumerated types. In the following, we show how to define different kinds of users of a system.
In LIGO, a constant constructor is equivalent to the same constructor
taking an argument of type
unit, so, for example,
Guest is the
same value as
option type is a 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
type would be
Some (v), where
v is some
meaningful value of any type. An example in arithmetic is the
Pattern matching is similiar to the
switch construct in
on the value of a variant. Consider for example the definition of a
flip that flips a coin.
You can call the function
flip by using the LIGO compiler like so: