Module Async_kernel.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.
type +'a t= 'a Async_kernel__.Deferred1.t
val sexp_of_t : ('a -> Ppx_sexp_conv_lib.Sexp.t) -> 'a t -> Ppx_sexp_conv_lib.Sexp.t
include Core_kernel.Invariant.S1 with type 'a t := 'a t
val invariant : 'a Base__.Invariant_intf.inv -> 'a t Base__.Invariant_intf.inv
val create : ('a Ivar.t -> unit) -> 'a tcreate fcallsf i, whereiis an empty ivar.createreturns a deferred that becomes determined whenffillsi.
val upon : 'a t -> ('a -> unit) -> unitupon t fwill runf vat some point aftertbecomes determined with valuev.
val peek : 'a t -> 'a optionpeek treturnsSome vifftis determined with valuev.
val value_exn : 'a t -> 'avalue_exn treturnsviftis determined with valuev, and raises otherwise.
val is_determined : 'a t -> boolis_determined treturnstrueifftis determined.
Deferreds form a monad.
let%bind v = t in f v returns a deferred t' that waits until t is determined with value v, at which point it waits for f v to become determined with value v', to which t' will become determined.
return v returns a deferred that is immediately determined with value v.
Note that:
upon t f is more efficient than:
ignore (let%bind a = t in f a; return ()) because upon, unlike let%bind, does not create a deferred to hold the result.
For example, one can write a loop that has good constant factors with:
let rec loop () =
upon t (fun a -> ... loop () ... ) although often forever or repeat_until_finished is more clear.
The same loop written with let%bind would allocate deferreds that would be immediately garbage collected. (In the past, this loop would have also used linear space in recursion depth!)
In general, for deferreds that are allocated by let%bind to be garbage collected quickly, it is sufficient that the allocating bind be executed in tail-call position of the right-hand side of an outer bind.
include Core_kernel.Monad with type 'a t := 'a t
include Base__.Monad_intf.S_without_syntax with type 'a t := 'a t
module Monad_infix : Base__.Monad_intf.Infix with type 'a t := 'a tval return : 'a -> 'a treturn vreturns the (trivial) computation that returns v.
val ignore_m : 'a t -> unit tignore_m tismap t ~f:(fun _ -> ()).ignore_mused to be calledignore, but we decided that was a bad name, because it shadowed the widely usedCaml.ignore. Some monads still dolet ignore = ignore_mfor historical reasons.
module Infix : sig ... endval unit : unit tunitis a deferred that is always determined with value()
val ignore : _ t -> unit tval never : unit -> _ tnever ()returns a deferred that never becomes determined.
val both : 'a t -> 'b t -> ('a * 'b) tboth t1 t2becomes determined after botht1andt2become determined.
val all : 'a t list -> 'a list tall tsreturns a deferred that becomes determined when everytints is determined. The output is in the same order as the input.
val any : 'a t list -> 'a tany tsreturns a deferred that is determined when any of the underlying deferreds is determined.
val any_unit : unit t list -> unit tany_unitis likeany, but ignores results of the component deferreds.
val don't_wait_for : unit t -> unitdon't_wait_for tignorest. It is likeFn.ignore, but is more constrained because it requires aunit Deferred.t.Rather than
ignore (t : _ t), dodon't_wait_for (Deferred.ignore t).We chose to give
don't_wait_fortypeunit trather than_ tto catch errors where a value is accidentally ignored.
module Choice : sig ... endA
Choice.tis used to produce an argument toenabledorchoose. See below.
type 'a choice= 'a Choice.t
val choice : 'a t -> ('a -> 'b) -> 'b Choice.tval enabled : 'b Choice.t list -> (unit -> 'b list) tenabled [choice t1 f1; ... choice tn fn;]returns a deferreddthat becomes determined when any of thetibecomes determined. The value ofdis a functionfthat when called, for eachtithat is enabled, appliesfitoti, and returns a list of the results. It is guaranteed that the list is in the same order as the choices supplied toenabled, but of course it may be shorter than the input list if not alltiare determined.
val choose : 'b Choice.t list -> 'b tchoose [ choice t1 f1 ; ... ; choice tn fn ]returns a deferred
tthat becomes determined with valuefi aiafter sometibecomes determined with valueai. It is guaranteed thatchoosecalls at most one of thefis, the one that determines its result. There is no guarantee that thetithat becomes determined earliest in time will be the one whose value determines thechoose. Nor is it guaranteed that the value intis the first value (in place order) fromchoicesthat is determined at the timetis examined.For example, in:
choose [ choice t1 (fun () -> `X1) ; choice t2 (fun () -> `X2) ] >>> function | `X1 -> e1 | `X2 -> e2it may be the case that both
t1andt2become determined, yete2actually 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 for_ : int -> to_:int -> do_:(int -> unit t) -> unit tfor_ start ~to_:stop ~do_:fis the deferred analog of:for i = start to stop do f i; done
val repeat_until_finished : 'state -> ('state -> [ `Repeat of 'state | `Finished of 'result ] t) -> 'result trepeat_until_finished initial_state frepeatedly runsfuntilfreturns`Finished. The first call tofhappens immediately whenrepeat_until_finishedis called.
val forever : 'state -> ('state -> 'state t) -> unitforever initial_state frepeatedly runsf, supplying the state returned to the next call tof.
val ok : 'a t -> ('a, _) Core_kernel.Result.t tUseful for lifting values from the
Deferred.tmonad to theResult.t Deferred.tmonad.
Deferred collections
These contain operations for iterating in a deferred manner over different collection types.
module Array = Async_kernel__.Deferred_arraymodule List = Async_kernel__.Deferred_listmodule Map = Async_kernel__.Deferred_mapmodule Memo = Async_kernel__.Deferred_memomodule Queue = Async_kernel__.Deferred_queuemodule Sequence = Async_kernel__.Deferred_sequenceError-carrying deferreds
These contain interfaces for working with deferred type containing error-aware types, like 'a Option.t Deferred.t, or 'a Or_error.t Deferred.t. These all include support for monadic programming.
module Option = Async_kernel__.Deferred_optionmodule Or_error = Async_kernel__.Deferred_or_errormodule Result = Async_kernel__.Deferred_result