ligo/gitlab-pages/docs/language-basics/unit-option-pattern-matching.md
Matej Šima 628d818163 Revert "Merge with dev"
This reverts commit 6ffe220d928dc3137496bcea0cc0f4d72edc2846.
2019-11-07 23:19:27 +00:00

2.5 KiB

id title
unit-option-pattern-matching Unit, Option, Pattern matching

Optionals are a programing pattern seen in OCaml. Since Michelson and LIGO are both inspired by OCaml, you'll have the option to use them in LIGO as well.

Type unit

Units in Michelson or LIGO represent for the lack of better words - an empty/useless/not needed value.

Here's how they're defined:

💡 Units come in handy when we try pattern matching on custom variants below.

const n: unit = Unit;
let n: unit = ()

Variants

Variant is a user-defined or a built-in type (in case of optionals) that can be compared to Enum (from javascript).

Here's how to define a new variant type:

type id is nat
type user is
| Admin of id
| Manager of id
| Guest;

const u: user = Admin(1000n);
const g: user = Guest(Unit);
type id = nat
type user =
| Admin of id
| Manager of id
| Guest of unit

let u: user = Admin 1000n
let g: user = Guest ()

Defining a varient can be extremely useful for building semantically appealing contracts. We'll learn how to use variants for 'logic purposes' shortly.

Optional values

Optionals are a type of built-in variant that can be used to determine if a variable holds a certain value or not. This is especially useful when (for example) your program's state allows for a certain variable value to be empty, like this:

type dinner is option(string);

// stay hungry
const p1: dinner = None;
// have some hamburgers
const p2: dinner = Some("Hamburgers")
type dinner = string option

let p1: dinner = None
let p2: dinner = Some "Hamburgers"

Pattern matching

Pattern matching is very similiar to e.g. switch in Javascript, and can be used to re-route the program's flow based on a value of a variant.

type dinner is option(string);
function is_hungry(const dinner: dinner): bool is block { skip } 
    with (
        case dinner of 
            | None -> True
            | Some(d) -> False
        end
    )
type dinner = string option
let is_hungry (d: dinner) : bool =
  match d with
  | None -> true
  | Some s -> false