Improve library facilities for handling `'a option tzresult Lwt.t`s
It's very common to see 'a option tzresult Lwt.t
s, and they're tricky to work with as a result of not having any functions specific to them.
For example, the following pattern is pervasive; suppose we have in our context
type a
type b
val mx: a option tzresult Lwt.t
val foo: a -> b tzresult Lwt.t
(* partial or impure *)
val fallback: unit -> b tzresult Lwt.t
then we might wish to evaluate
let bar: b tzresult Lwt.t =
mx >>=? function
| Some x -> foo x
| None -> fallback()
This could be addressed with a carefully thought out library for 'a option tzresult Lwt.t
.
A submodule Error_monad.Option
might be a good idea.
for example, if we might have something like had
module Error_monad
...
module Option : sig
val (>>?): 'a option tzresult -> ('a -> 'b option tzresult) -> 'b option tzresult
val (>>=?): 'a option tzresult Lwt.t -> ('a -> 'b option tzresult Lwt.t) -> 'b option tzresult Lwt.t
(* The mapping is `'a -> 'b tzresult` because we don't eliminate `tzresult` *)
val unopt_map_lazy:
f:('a -> 'b tzresult)
-> default:(unit -> 'b tzresult)
-> 'a option tzresult
-> 'b tzresult
(* The mapping is `'a -> 'b tzresult Lwt.t` because we don't eliminate `tzresult` or `Lwt.t` *)
module Lwt: sig
val unopt_map_lazy:
f:('a -> 'b tzresult Lwt.t)
-> default:(unit -> 'b tzresult Lwt.t)
-> 'a option tzresult Lwt.t
-> 'b tzresult Lwt.t
end
end
then the example would become
let bar: b tzresult Lwt.t =
Error_monad.Option.Lwt.unopt_map_lazy ~f:foo ~default:fallback mx
Edited by Sylvain R.