package lwt_ppx
Install
dune-project
Dependency
Authors
Maintainers
Sources
md5=d0f824f75ce5297975aec75366fed36c
sha512=c43fff2e9e808dc49ddf09caf7040e512ba36aebe2c600d524095ced11c196c3ac619953a11cb3e3a9f7c6c99e798d43bf179d5f790ab258fb9f746fae4c1125
doc/ppx_lwt/Ppx_lwt/index.html
Module Ppx_lwtSource
Ppx syntax extension for Lwt
Ppx extensions
This Ppx extension adds various syntactic shortcut for lwt programming. It needs ppx_tools.
To use it, simply use the ocamlfind package lwt_ppx.
This extension adds the following syntax:
- lwt-binding:
let%lwt ch = get_char stdin in
codeis the same as bind (get_char stdin) (fun ch -> code).
Moreover, it supports parallel binding:
let%lwt x = do_something1 ()
and y = do_something2 in
codewill run do_something1 () and do_something2 (), then bind their results to x and y. It is the same as:
let t1 = do_something1
and t2 = do_something2 in
bind t1 (fun x -> bind t2 (fun y -> code))Due to a bug in the OCaml parser, if you'd like to put a type constraint on the variable, please write
let (foo : int) = do_something in
codeNot using parentheses will confuse the OCaml parser.
- exception catching:
try%lwt
<expr>
with
<branches>For example:
try%lwt
f x
with
| Failure msg ->
prerr_endline msg;
return ()is expanded to:
catch (fun () -> f x)
(function
| Failure msg ->
prerr_endline msg;
return ()
| exn ->
Lwt.reraise exn)Note that the exn -> Lwt.reraise exn branch is automatically added when needed.
- finalizer:
(<expr>) [%finally <expr>]You can use [%lwt.finally ...] instead of [%finally ...].
- assertion:
assert%lwt <expr>- for loop:
for%lwt i = <expr> to <expr> do
<expr>
doneand:
for%lwt i = <expr> downto <expr> do
<expr>
done- while loop:
while%lwt <expr> do
<expr>
done- pattern matching:
match%lwt <expr> with
| <patt_1> -> <expr_1>
...
| <patt_n> -> <expr_n>Exception cases are also supported:
match%lwt <expr> with
| exception <exn> -> <expr_1>
| <patt_2> -> <expr_2>
...
| <patt_n> -> <expr_n>- conditional:
if%lwt <expr> then
<expr_1>
else
<expr_2>and
if%lwt <expr> then <expr_1>