package octez-libs
Syntax module for Lwt+Option. This is intended to be opened locally in functions which use Lwt and option
for control-flow. Within the scope of this module, the code can include binding operators, leading to a let
-style syntax.
val return : 'a -> 'a option Lwt.t
return x
is Lwt.return (Some x)
.
val return_unit : unit option Lwt.t
return_unit
is Lwt.return (Some ())
.
val return_nil : 'a list option Lwt.t
return_nil
is Lwt.return (Some [])
.
val return_true : bool option Lwt.t
return_true
is Lwt.return (Some true)
.
val return_false : bool option Lwt.t
return_false
is Lwt.return (Some false)
.
Note that we do not provide return_some
nor return_none
. Both of these functions are possible but somewhat confusing and rarely useful in practice. If you need to carry option
s within a LwtOption-monad computation (yielding values of the type 'a option option Lwt.t
), you need to do so by hand: return (Some …)
and return (None)
.
val fail : 'a option Lwt.t
fail
is Lwt.return None
.
let*
is a binding operator alias for Lwt.bind
mixed with Option.bind
.
and*
is a binding operator alias for both
.
let+
is a binding operator alias for Lwt.map
mixed with Option.map
.
and*
is a binding operator alias for both
.
The following values are for mixing expressions that are Lwt-only or Option-only within the Lwt option monad. Note that there are fundamental differences between option
and Lwt.t
: the former can be simply matched on (i.e., it is possible to get out of the monad at any point) whereas the latter can only be bound on (i.e., it is not possible to get out of the monad). In addition, the former is for aborting computations on failures (which in this case means that no value can be produced) whereas the latter is for waiting before continuing.
Still, from a syntax point-of-view, both are handled the same way: with a specialised binding operator.
let*!
is for binding Lwt-only expressions into the Lwt option combined monad.
let open Lwt_option_syntax in
let* x = … in
let*! y = … in
return (x + y)
let*?
is for binding the value from Option-only expressions into the Lwt option combined monad.
let open Lwt_option_syntax in
let* x = … in
let*? y = … in
…