Up

Module Deferred

A value that will become determined asynchronously.

A deferred can be "undetermined" or "determined". A deferred that is undetermined may at some point become determined with value v, and will henceforth always be determined with value v.

Signature

module Array : module type of Deferred_array
module List : module type of Deferred_list
module Map : module type of Deferred_map
module Memo : module type of Deferred_memo
module Option : module type of Deferred_option
module Or_error : module type of Deferred_or_error
module Queue : module type of Deferred_queue
module Result : module type of Deferred_result
module Sequence : module type of Deferred_sequence
type +'a t = 'a Deferred1.t
val sexp_of_t : ('a -> Sexplib.Sexp.t) -> 'a t -> Sexplib.Sexp.t
include Core_kernel.Std.Invariant.S1 with type 'a t := 'a t
type 'a t
val invariant : 'a Invariant_intf.inv -> 'a t Invariant_intf.inv

sexp_of_t t f returns a sexp of the deferred's value, if it is determined, or an informative string otherwise.

This is just for display purposes. There is no t_of_sexp.

val create : ('a Ivar.t -> unit) -> 'a t

create f calls f i, where i is empty ivar. create returns a deferred that becomes determined when f fills i.

val upon : 'a t -> ('a -> unit) -> unit

upon t f will run f v at some point after t becomes determined with value v.

val peek : 'a t -> 'a option

peek t returns Some v iff t is determined with value v.

val value_exn : 'a t -> 'a

value_exn t returns v if t is determined with value v, and raises otherwise.

val is_determined : 'a t -> bool

is_determined t returns true iff t is determined.

include Core_kernel.Std.Monad 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
module Infix : sig .. end
val unit : unit t

unit is a deferred that is always determined with value ()

val ignore : _ t -> unit t
val never : unit -> _ t

never () returns a deferred that never becomes determined

val both : 'a t -> 'b t -> ('a * 'b) t

both t1 t2 becomes determined after both t1 and t2 become determined.

val all : 'a t list -> 'a list t

all ts returns a deferred that becomes determined when every t in ts is determined. The output is in the same order as the input.

val all_unit : unit t list -> unit t

Like all, but ignores results of the component deferreds

val any : 'a t list -> 'a t

any ts returns a deferred that is fulfilled when any of the underlying deferreds is fulfilled

val any_unit : 'a t list -> unit t

any_unit ts like any but ignores results of the component deferreds

val don't_wait_for : unit t -> unit

don't_wait_for t ignores t. It is like Fn.ignore, but is more constrained because it requires a unit Deferred.t.

Rather than ignore (t : _ t), do don't_wait_for (Deferred.ignore t).

We chose to give don't_wait_for type unit t rather than _ t to catch errors where a value is accidentally ignored.

type +'a choice = 'a Deferred1.choice

choice is used to produce an argument to enabled or choose. See below.

val choice : 'a t -> ('a -> 'b) -> 'b choice
val enabled : 'b choice list -> (unit -> 'b list) t

enabled [choice t1 f1; ... choice tn fn;] returns a deferred d that becomes determined when any of the ti become determined. The value of d is a function f that when called, for each ti that is enabled, applies fi to ti, and returns a list of the results. It is guaranteed that the list is in the same order as the choices supplied to enabled, but of course it may be shorter than the input list if not all ti are determined.

val choose : 'b choice list -> 'b t

     choose [ choice t1 f1
            ; ...
            ; choice tn fn
            ]
   

returns a deferred t that becomes determined with value fi ai after some ti becomes determined with value ai. It is guaranteed that choose calls at most one of the fis, the one that determines its result. There is no guarantee that the ti that becomes determined earliest in time will be the one whose value determines the choose. Nor is it guaranteed that the value in t is the first value (in place order) from choices that is determined at the time t is examined.

For example, in:


     choose [ choice t1 (fun () -> `X1)
            ; choice t2 (fun () -> `X2)
            ]
     >>> function
     | `X1 -> e1
     | `X2 -> e2
   

it may be the case that both t1 and t2 become determined, yet e2 actually runs.

It is guaranteed that if multiple choices are determined with no intervening asynchrony, then the earliest choice in the list will become the value of the choose.

val repeat_until_finished : 'state -> ('state -> [
| `Repeat of 'state
| `Finished of 'result
] t) -> 'result t

repeat_until_finished initial_state f repeatedly runs f until f returns `Finished. The first call to f happens immediately when repeat_until_finished is called.

val forever : 'state -> ('state -> 'state t) -> unit

forever initial_state f repeatedly runs f, supplying the state returned to the next call to f.