package sugar

  1. Overview
  2. Docs

This interface specifies an error handling layer for monadic computations.

Sugar value modules work with any monad.

module MyMonad = struct
  type 'a monad = 'a Lwt.t
  let return = Lwt.return
  let (>>=) = Lwt.bind
end
module MyResult = Sugar.Promise.Make (MyMonad) (MyError)
include Promise
type error

Error definition imported from your project

type 'a value = ('a, error) Result.result

An alias for Pervasives.result that can only work with errors of your project.

type 'a monad

This type is an alias to the underlining monad used to create your Promise module. In other words, it points to the main type of your threading library (Lwt.t for Lwt, or Async.Std.Deferred.t for Async).

type 'a result = 'a value monad

This type describes a result monad inside a project.

For example, if the concrete module is built on top of Lwt, a value of type unit result will be translated as (unit, error) Pervasives.result Lwt.t.

val (>>=) : 'a result -> ('a -> 'b result) -> 'b result

This combinator is also called bind.

It can be used to chain sequential operations with the current monad. If the computation in the left failed, the operator will propagate the error, skipping the function completely.

val bind : 'a result -> ('a -> 'b result) -> 'b result

Similar to S.Result.bind

val bind_unless : 'a result -> (error -> 'a result) -> 'a result
val map : 'a result -> ('a -> 'b) -> 'b result

Similar to S.Result.map

val return : 'a -> 'a result

Similar to S.Result.return

val throw : error -> 'a result

Similar to S.Result.throw

module Infix : sig ... end
val unwrap : 'a value monad -> 'a monad

Unwraps the successful value as a normal value in the threading monad. If the value is not successful, it will raise an Invalid_arg exception.

val unwrap_or : (error -> 'a monad) -> 'a value monad -> 'a monad

Unwraps the successful value as a value in the threading monad. Different from unwrap, you can assign an error handler to be executed if the computation failed.

val expect : 'a value monad -> string -> 'a monad

Extracts a successful value from an computation, or raises and Invalid_arg exception with a customized error message.

module NoExceptions : Promise with type error := error and type 'a monad := 'a monad

Disable exception handling. Open this module with you don't want to catch exceptions.