Up

Module Deferred_or_error

The deferred analog of Core.Or_error. It is exposed in std.ml as Deferred.Or_error.

The mental model for a function returning an 'a Deferred.Or_error.t is that the function never raises. All error cases are caught and expressed as an Error _ result. This module preserves that property.

Unfortunately, there is no way to enforce this property using the type system, so it is more like a convention, or idiom. A function whose type ends with ... -> 'a Deferred.Or_error.t and still raises should be considered broken, and be fixed. With that property in mind, Deferred.Or_error.List.iter, for example, does not wrap the execution of the given iter function f inside a monitor. If one of these application raises, the whole function Deferred.Or_error.List.iter will raise as a way to try to alert the developer that one the function is broken and needs attention and fixing, rather than silently catching the error and converting it to Or_error.Error.

This behavior is consistent with Core.Or_error's treatment of user-supplied functions.

If you have to deal with a function that does not respect this idiom, you can use Deferred.Or_error.try_with_join to wrap its execution and enforce this property.

Signature

include Core_kernel.Std.Applicative.S with type 'a t := 'a t
type 'a t
val return : 'a -> 'a t
val apply : ('a -> 'b) t -> 'a t -> 'b t
val map : 'a t -> f:('a -> 'b) -> 'b t
val map2 : 'a t -> 'b t -> f:('a -> 'b -> 'c) -> 'c t
val map3 : 'a t -> 'b t -> 'c t -> f:('a -> 'b -> 'c -> 'd) -> 'd t
val all : 'a t list -> 'a list t
val both : 'a t -> 'b t -> ('a * 'b) t
module Applicative_infix : sig .. end
include module type of Applicative_infix
val (<*>) : ('a -> 'b) t -> 'a t -> 'b t

same as apply

val (<*) : 'a t -> unit t -> 'a t

same as apply

val (*>) : unit t -> 'a t -> 'a t
include Core_kernel.Std.Monad.S with type 'a t := 'a t
type 'a t
include Monad_intf.S_without_syntax with type 'a t := 'a t
type 'a t

A monad is an abstraction of the concept of sequencing of computations. A value of type 'a monad represents a computation that returns a value of type 'a.

include Monad_intf.Infix with type 'a t := 'a t
type 'a t
val (>>=) : 'a t -> ('a -> 'b t) -> 'b t

t >>= f returns a computation that sequences the computations represented by two monad elements. The resulting computation first does t to yield a value v, and then runs the computation returned by f v.

val (>>|) : 'a t -> ('a -> 'b) -> 'b t

t >>| f is t >>= (fun a -> return (f a)).

module Monad_infix : Monad_intf.Infix with type 'a t := 'a t
val bind : 'a t -> ('a -> 'b t) -> 'b t

bind t f = t >>= f

val return : 'a -> 'a t

return v returns the (trivial) computation that returns v.

val map : 'a t -> f:('a -> 'b) -> 'b t

map t ~f is t >>| f.

val join : 'a t t -> 'a t

join t is t >>= (fun t' -> t').

val ignore_m : 'a t -> unit t

ignore_m t is map t ~f:(fun _ -> ()). ignore_m used to be called ignore, but we decided that was a bad name, because it shadowed the widely used Pervasives.ignore. Some monads still do let ignore = ignore_m for historical reasons.

val all : 'a t list -> 'a list t
val all_ignore : unit t list -> unit t
include Monad_intf.Syntax with type 'a t := 'a t
type 'a t
module Let_syntax : sig .. end
val fail : Core_kernel.Std.Error.t -> _ t

fail error = Deferred.return (Error error) *

val ignore : _ t -> unit t
val ok_exn : 'a t -> 'a Deferred.t

These functions are direct analogs of the corresponding Core.Or_error functions.

val of_exn : exn -> _ t
val of_exn_result : ('a, exn) Core_kernel.Std.Result.t Deferred.t -> 'a t
val error : string -> 'a -> ('a -> Core_kernel.Std.Sexp.t) -> _ t
val error_string : string -> _ t
val errorf : ('a, unit, string, _ t) Core_kernel.Std.format4 -> 'a
val tag : 'a t -> string -> 'a t
val tag_arg : 'a t -> string -> 'b -> ('b -> Core_kernel.Std.Sexp.t) -> 'a t
val unimplemented : string -> _ t
val combine_errors : 'a t list -> 'a list t
val combine_errors_unit : unit t list -> unit t
val ok_unit : unit t

ok_unit = return ()

val try_with : ?extract_exn:bool -> ?name:string -> (unit -> 'a Deferred.t) -> 'a t

try_with f catches exceptions thrown by f and returns them in the Result.t as an Error.t. try_with_join is like try_with, except that f can throw exceptions or return an Error directly, without ending up with a nested error; it is equivalent to try_with f >>| Result.join.

The option extract_exn is passed along to Monitor.try_with ?extract_exn and specifies whether or not the monitor exn wrapper should be skipped (extract_exn:true or kept (extract_exn:false).

val try_with_join : ?extract_exn:bool -> ?name:string -> (unit -> 'a t) -> 'a t
module List : Monad_sequence.S with type 'a monad := 'a t with type 'a t := 'a list