Module maybe

Group by:


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


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] {.

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.


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
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
eval expr2