options
nimony/lib/std/options.nim
This module implements types which encapsulate an optional value.
A value of type Option[T] either contains a value x (represented as some(x)) or is empty (none(T)).
This is the Nimony port. Since exceptions are not yet wired up, get on an empty Option terminates the program (mirroring assert) rather than raising UnpackDefect. Equality is the compiler-provided structural comparison: an empty Option always stores the default value, so two empty Options compare equal and an empty one never equals a populated one.
type Option = object val: T has: bool
func some(val: sink T): Option[T]Returns an
Optionthat containsval.func none(): Option[T]Returns an empty
Option(i.e. with no value).func option(val: sink T): Option[T]Wraps
valin anOption; for value types this is simplysome(val).func isSome(self: Option[T]): boolReturns
trueifselfcontains a value.func isNone(self: Option[T]): boolReturns
trueifselfis empty.func unsafeGet(self: Option[T]): TReturns the contained value. Behaviour is undefined when
selfis empty; only use after checkingisSome.func get(self: Option[T]): TReturns the contained value. Terminates the program if
selfis empty.func get(self: Option[T]; otherwise: T): TReturns the contained value, or
otherwiseifselfis empty.func $(self: Option[T]): stringReturns the string representation of
self.proc map(self: Option[T]; cb: proc (x: T): R): Option[R]Applies
cbto the contained value and wraps the result; empty stays empty.proc filter(self: Option[T]; cb: proc (x: T): bool): Option[T]Returns
selfif it holds a value satisfyingcb, otherwise empty.proc flatMap(self: Option[T]; cb: proc (x: T): Option[R]): Option[R]Applies
cb(which itself returns anOption) and flattens the result.func flatten(self: Option[Option[T]]): Option[T]Collapses a nested
Optioninto a single one.