5.7. Monadic Option<T> — a value-or-nothing tagged pair

Monadic Option<T> — represents a value that may or may not be present. Functional API for modelling “absence” in ordinary value code, where nullable pointers are inapplicable and sentinel values (-1, "") are fragile.

The payload may be any type, including non-copyable ones (array<T>, table<K;V>, lambdas): constructors and combinators clone or move on the non-copyable branch via static_if (typeinfo can_copy(...)).

Construct via some(v) (clones), move_some(v) (moves out of a mutable source), or none(type<T>); read via unwrap / unwrap_or / ??; compose via map / and_then / filter / or_else through the |> pipe.

See also Option<T> and Result<T, E> and daslib/result for the Result<T, E> counterpart.

5.7.1. Structures

Option

struct Option

5.7.2. Constructors

move_some(v: auto(TT)): auto

def move_some (var v: auto(TT)) : auto

Arguments:
  • v : auto(TT)

none(t: auto(TT)): auto

def none (t: auto(TT)) : auto

Arguments:
  • t : auto(TT)

some(v: auto(TT)): auto

def some (v: auto(TT)) : auto

Arguments:
  • v : auto(TT)

5.7.3. Queries

is_none(o: $Option(type<auto(TT)>)): bool

def is_none (o: $Option(type<auto(TT)>)) : bool

Arguments:
  • o : typemacro

is_some(o: $Option(type<auto(TT)>)): bool

def is_some (o: $Option(type<auto(TT)>)) : bool

Arguments:
  • o : typemacro

5.7.4. Transforming

and_then(o: $Option(type<auto(TT)>); f: block<(v:TT):$Option(type<auto(UU)>)>): auto

def and_then (o: $Option(type<auto(TT)>); f: block<(v:TT):$Option(type<auto(UU)>)>) : auto

Arguments:
  • o : typemacro

  • f : block<(v:TT):typemacro>

filter(o: $Option(type<auto(TT)>); p: block<(v:TT):bool>): auto

def filter (o: $Option(type<auto(TT)>); p: block<(v:TT):bool>) : auto

Arguments:
  • o : typemacro

  • p : block<(v:TT):bool>

map(o: $Option(type<auto(TT)>); f: block<(v:TT):auto(UU)>): auto

def map (o: $Option(type<auto(TT)>); f: block<(v:TT):auto(UU)>) : auto

Arguments:
  • o : typemacro

  • f : block<(v:TT):auto(UU)>

or_else(o: $Option(type<auto(TT)>); f: block<():void>): auto

def or_else (o: $Option(type<auto(TT)>); f: block<():void>) : auto

Arguments:
  • o : typemacro

  • f : block<void>

or_value(o: $Option(type<auto(TT)>); v: TT): auto

def or_value (o: $Option(type<auto(TT)>); v: TT) : auto

Arguments:
  • o : typemacro

  • v : TT

5.7.5. Extraction

expect_value(o: $Option(type<auto(TT)>); msg: string): TT

def expect_value (o: $Option(type<auto(TT)>); msg: string) : TT

Arguments:
  • o : typemacro

  • msg : string

move_unwrap(o: $Option(type<auto(TT)>)): TT

def move_unwrap (var o: $Option(type<auto(TT)>)) : TT

Arguments:
  • o : typemacro

unwrap(o: $Option(type<auto(TT)>)): TT

def unwrap (o: $Option(type<auto(TT)>)) : TT

Arguments:
  • o : typemacro

unwrap_or(o: $Option(type<auto(TT)>); d: TT): TT

def unwrap_or (o: $Option(type<auto(TT)>); d: TT) : TT

Arguments:
  • o : typemacro

  • d : TT

unwrap_or_default(o: $Option(type<auto(TT)>)): TT

def unwrap_or_default (o: $Option(type<auto(TT)>)) : TT

Arguments:
  • o : typemacro

unwrap_or_else(o: $Option(type<auto(TT)>); f: block<():void>): TT

def unwrap_or_else (o: $Option(type<auto(TT)>); f: block<():void>) : TT

Arguments:
  • o : typemacro

  • f : block<void>

5.7.6. Side effects

if_none(o: $Option(type<auto(TT)>); f: block<():void>)

def if_none (o: $Option(type<auto(TT)>); f: block<():void>)

Arguments:
  • o : typemacro

  • f : block<void>

if_some(o: $Option(type<auto(TT)>); f: block<(v:TT):void>)

def if_some (o: $Option(type<auto(TT)>); f: block<(v:TT):void>)

Arguments:
  • o : typemacro

  • f : block<(v:TT):void>

5.7.7. Pairing

zip(a: $Option(type<auto(TT)>); b: $Option(type<auto(UU)>)): auto

def zip (a: $Option(type<auto(TT)>); b: $Option(type<auto(UU)>)) : auto

Arguments:
  • a : typemacro

  • b : typemacro

5.7.8. Operators

$Option(type<auto(TT)>)!=(o: $Option(type<auto(TT)>); v: TT): bool

def $Option(type<auto(TT)>)!= (o: $Option(type<auto(TT)>); v: TT) : bool

Arguments:
  • o : typemacro

  • v : TT

5.7.8.1. $Option(type<auto(TT)>)==

$Option(type<auto(TT)>)==(a: $Option(type<auto(TT)>); b: $Option(type<auto(TT)>)): bool

def $Option(type<auto(TT)>)== (a: $Option(type<auto(TT)>); b: $Option(type<auto(TT)>)) : bool

Arguments:
  • a : typemacro implicit

  • b : typemacro implicit

$Option(type<auto(TT)>)==(o: $Option(type<auto(TT)>); v: TT): bool

$Option(type<auto(TT)>)??(o: $Option(type<auto(TT)>); d: TT): TT

def $Option(type<auto(TT)>)?? (o: $Option(type<auto(TT)>); d: TT) : TT

Arguments:
  • o : typemacro

  • d : TT

auto(TT)!=(v: auto(TT); o: $Option(type<TT>)): bool

def auto(TT)!= (v: auto(TT); o: $Option(type<TT>)) : bool

Arguments:
  • v : auto(TT)

  • o : typemacro

auto(TT)==(v: auto(TT); o: $Option(type<TT>)): bool

def auto(TT)== (v: auto(TT); o: $Option(type<TT>)) : bool

Arguments:
  • v : auto(TT)

  • o : typemacro