Module maybe

Search:
Group by:

Types

Maybe*[T] = object
  case valid*: bool
  of true: value: T
  of false: nil
  

Procs

proc `==`*[T](m1: Maybe[T]; m2: Maybe[T]): bool
Equality for maybe objects
proc nothing*[T](): Maybe[T]
Construct a maybe instance in the invalid state.
proc just*[T](val: T): Maybe[T]
Construct a maybe instance in the valid state.
proc `$`*[T](m: Maybe[T]): string
Convert a maybe instance to a string.
proc fmap*[T, U](m: Maybe[U]; p: (U -> T)): Maybe[T] {.
procvar
.}

Used to map a function over a boxed value.

Equivalent to (Functor f) => fmap :: (a->b) -> f a -> f b in Haskell.

proc `>>=`*[T, U](m: Maybe[U]; p: (U -> Maybe[T])): Maybe[T]

Used for chaining monadic computations together.

Analagous to bind(>>=) in Haskell.

Macros

macro maybeCase*[T](m: Maybe[T]; body: untyped): untyped

A macro which provides a safe access pattern to the maybe type. This avoids the need to have a get function which throws an exception when its used improperly.

This makes the following conversion

maybeCase m:
just x:
expr1 using x
nothing:
expr2 that does not use x(trying to refer to x will not compile)

converts to --->

if m.isValid:
eval expr using x where x is replaced with m.value
else:
eval expr2