I looked for user-facing documentation on Lwt_direct and did not find much. In particular, the Lwt manual appears to be stuck on 5.5.0 for now (and the opam packages do not come with documentation on say lwt_direct 6.0.0 (latest) · OCaml Package ). What is the plan for non-expert users to learn how to use the new direct-style approach?
Edit: for now I suppose the documentation is to be found directly (no pun intended) in lwt_direct.mli, which is well-commented.
the wiki update for manual requires manual intervention and hasn’t been updated in a while. beyond the API documentation, other parts need to be rewritten as well. i’ll put a notice on there that the documentation is generated on ocaml.org
it seems that the ocaml.org documentation failed with some unrelated error, i’ve opened an issue on the ocaml.org bug tracker
Thanks @raphael-proust, I look forward to experimenting with this.
One question re: the example in the lwt_direct.mli header linked above:
open Lwt_direct
spawn (fun () ->
let continue = ref true in
while !continue do
match await @@ Lwt_io.read_line in_channel with
| exception End_of_file -> continue := false
| line ->
let uppercase_line = String.uppercase_ascii line in
await @@ Lwt_io.write_line out_channel uppercase_line
done)
]}
My assumption is that the use of a reference here is just because of the while-loop, and doesn’t have anything to do with non-local control. So presumably this example could also be implemented using tail recursion, for example. Is this correct?
Yes, this is correct. The example could use a recursive function. However, I think that for the purpose of demonstrating the power of lwt-direct, the while loop is better:
you can use recursive functions with lwt
you can’t use while with lwt because it expects the body to be of type unit
(You can use while%lwt with the syntax extension though, but not just plain while.)
So the choice of while is for demonstration purpose.
In my opinion, an even better example of the power of lwt-direct is
let iter_s f h =
Lwt_direct.spawn (fun () ->
Hashtbl.iter (fun k v -> Lwt_direct.await @@ f k v) h
It shows how you can use lwt functions with a library which expects non-lwt arguments.