Module Sugar__.Option

An implementation of Result interface for the option type.

This is probably the easiest way to start using Sugar, as there is no need to use describe errors. Still, because this module follows the same interface, when you need to start refactoring to more complex, monadic results you get to keep the same clean interface, making be transition straightfoward.

Usage example:


    open Sugar.Option

    let do_something (): string result =
      if true then
        Some "you could use any option type"
      else
        throw ()

    let run (): string result =
      do_something ()
      >----------
      ( fun () ->
        return "recovered"
      )
  

In case you are wondering, the evaluation of run () in the example above, will produce: string option = Some "you could use any option type".

type error = unit
type 'a result = 'a option
val return : 'a ‑> 'a option
val throw : unit ‑> 'a option
val bind : 'a option ‑> ('a ‑> 'b option) ‑> 'b option
val bind_unless : 'a option ‑> (unit ‑> 'a option) ‑> 'a option
val map : 'a option ‑> ('a ‑> 'b) ‑> 'b option
val (>>=) : 'a option ‑> ('a ‑> 'b option) ‑> 'b option
val (>>) : unit option ‑> 'a option ‑> 'a option
module Infix : sig ... end
val wrap : (unit ‑> 'a) ‑> 'a option
val unwrap : 'a option ‑> 'a
val unwrap_or : 'a option ‑> (unit ‑> 'a) ‑> 'a
val expect : 'a option ‑> string ‑> 'a
module Monad : Sugar.S.Params.Monad with type 'a t = 'a option