package sugar

  1. Overview
  2. Docs

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 -> 'b option
val throw : unit -> 'a option
val bind : 'a option -> ('b -> 'c option) -> 'c option
val bind_unless : 'a option -> (unit -> 'b option) -> 'b option
val map : 'a option -> ('b -> 'c) -> 'd option
val (>>=) : 'a option -> ('a -> 'b option) -> 'b option
val (>>) : unit option -> 'a option -> 'a option
module Infix : sig ... end
val wrap : (unit -> 'a) -> 'b option
val unwrap : 'a option -> 'b
val unwrap_or : 'a option -> (unit -> 'b) -> 'c
val expect : 'a option -> string -> 'b
module Monad : S.Params.Monad with type 'a t = 'a option
OCaml

Innovation. Community. Security.