123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114openS.Params(**
How to create a {{!Sugar.S.Promise}promise-based} result monad:
{[
module MyError = struct
type t = A | B | C
end
module MyMonad = struct
type 'a t = 'a Lwt.t
let return = Lwt.return
let (>>=) = Lwt.(>>=)
end
module MyResult = Sugar.Promise.Make (MyError) (MyMonad)
]}
Notice that for most cases, you can just plug the module
directly from a monadic library, like:
{[
module MyResult = Sugar.Promise.Make (MyError) (Lwt)
module MyResult2 = Sugar.Promise.Make (MyError) (Async.Std.Deferred)
]}
*)(**
A parametric module that implements the monadic interface for values.
The complete documentation can be found in {!Sugar.S.Promise}.
*)moduleMake(UserError:Error)(UserMonad:Monad):S.Promisewithtypeerror:=UserError.tandtype'amonad:='aUserMonad.tandtype'avalue=('a,UserError.t)Result.resultandtype'aresult=('a,UserError.t)Result.resultUserMonad.t=structincludeUserErrortype'amonad='aUserMonad.ttype'avalue=('a,UserError.t)Result.resulttype'aresult='avaluemonadopenUserMonadopenResultletreturnv=UserMonad.return(Okv)letthrowe=UserMonad.return(Errore)letbindrf=r>>=function|Errore->throwe|Okv->fvletbind_unlessrf=r>>=function|Errore->fe|Okv->returnvletmaprf=r>>=function|Errore->throwe|Okv->return(fv)moduleInfix=structlet(>>=)=bindlet(>>|)=maplet(>>)xy=bindx(fun()->Lazy.forcey)let(>>>)xy=bindx(fun_->Lazy.forcey)let(>>>=)=UserMonad.(>>=)let(>---------)=bind_unlesslet(<*>)fx=f>>=funf'->x>>=funx'->return(f'x')let(<$>)fx=mapxfendletunwrapr=r>>=function|Okv->UserMonad.returnv|Error_->invalid_arg"Could not unwrap value"letunwrap_orfr=r>>=function|Okv->UserMonad.returnv|Errore->feletexpectrmsg=r>>=function|Okv->UserMonad.returnv|Error_->invalid_argmsglet(>>=)=bindend