package async_kernel
Install
Dune Dependency
Authors
Maintainers
Sources
sha256=0eda59386235e967698834d71cb8924d7b466bc4fcbf26ae72797ad05ca6f8a9
doc/async_kernel/Async_kernel/Deferred/Or_error/index.html
Module Deferred.Or_error
Source
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 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.
The applicative operations match the behavior of the applicative operations in Or_error
. This means that all
and all_unit
are equivalent to combine_errors
and combine_errors_unit
respectively.
include Core.Applicative.S with type 'a t := 'a t
module Applicative_infix : sig ... end
return x = Deferred.return (Ok x)
*
fail error = Deferred.return (Error error)
*
These functions are direct analogs of the corresponding Core.Or_error
functions.
val of_exn_result :
?backtrace:[ `Get | `This of string ] ->
('a, exn) Core.Result.t Deferred.t ->
'a t
find_map_ok l ~f
returns the first value in l
for which f
returns Ok
, otherwise it returns the same error as combine_errors (Deferred.List.map l ~f)
.
val try_with :
?extract_exn:bool ->
?run:[ `Now | `Schedule ] ->
?rest:[ `Log | `Raise | `Call of exn -> unit ] ->
?here:Lexing.position ->
?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
).
The ~rest
argument controls how exceptions are handled after the try_with
deferred becomes determined. They may be logged, raised, or passed to a callback.
The ~run
argument controls when f
gets called. `Now
calls f
immediately; `Schedule
schedules an asynchronous job to run f
.
val try_with_join :
?extract_exn:bool ->
?run:[ `Now | `Schedule ] ->
?rest:[ `Log | `Raise | `Call of exn -> unit ] ->
?here:Lexing.position ->
?name:string ->
(unit -> 'a t) ->
'a t
All of the List
functions that take a how
argument treat it the following way:
val repeat_until_finished :
'state ->
('state -> [ `Repeat of 'state | `Finished of 'result ] t) ->
'result t
repeat_until_finished initial_state f
works just like Deferred.repeat_until_finished
but with the Deferred.Or_error
monad. If f
returns an Or_error.Error
the loop terminates and returns.