Lwt: Running Examples

Created on 22 Apr 2020  ·  5Comments  ·  Source: ocsigen/lwt

Trying to run code.ml

let () =
  Lwt_main.run begin
    let%lwt file = Lwt_io.(open_file Input "code.ml") in
    begin
      let%lwt content = Lwt_io.read file in
      Lwt_io.print content
    end
    [%lwt.finally
      Lwt_io.close file]
  end

With the indicated command:

ocamlfind opt -linkpkg -package lwt_ppx,lwt.unix code.ml && ./a.out

Produces:

~/Github/reason-native*
❯ ocamlfind opt -linkpkg -package lwt_ppx,lwt.unix code.ml && ./a.out
ocamlfind: [WARNING] Package `threads': Linking problems may arise because of the missing -thread or -vmthread switch
File "code.ml", line 1:
Error: No implementations provided for the following modules:
         Mutex referenced from /Users/mandalarian/.opam/4.09.0/lib/lwt/unix/lwt_unix.cmxa(Lwt_main)
❯ ocamlfind opt -linkpkg -package lwt_ppx,lwt.unix code.ml && ./a.out
ocamlfind: [WARNING] Package `threads': Linking problems may arise because of the missing -thread or -vmthread switch
File "code.ml", line 1:
Error: No implementations provided for the following modules:
         Mutex referenced from /Users/mandalarian/.opam/4.09.0/lib/lwt/unix/lwt_unix.cmxa(Lwt_main)

Running with dune exec ./code gets you:

~/Downloads/code-example
❯ dune exec ./code.exe                                                
Info: Creating file dune-project with this contents:
| (lang dune 2.5)
File "code.ml", line 3, characters 8-11:
3 |     let%lwt file = Lwt_io.(open_file Input "code.ml") in
            ^^^
Error: Uninterpreted extension 'lwt'.

Have I done anything obviously wrong? Thank you for any guidance.

dune file is:

(executable
 (name code)
 (libraries lwt_ppx lwt.unix ))

All 5 comments

Seeing this now, https://github.com/ocsigen/lwt/issues/728. I guess the examples have to be updated?

Thanks, I've updated the docs in lwt.mli.

With the indicated command:

ocamlfind opt -linkpkg -package lwt_ppx,lwt.unix code.ml && ./a.out

As the ocamlfind error message suggests, the command now needs the -thread flag added:

https://github.com/ocsigen/lwt/blob/b134d880a10225eb057dfc8cf910321701514876/src/core/lwt.mli#L718

Running with dune exec ./code gets you:

To run examples that use let%lwt with Dune, you need to move lwt_ppx from libraries to (preprocess (pps lwt_ppx)):

(executable
 (name code)
 (preprocess (pps lwt_ppx))
 (libraries lwt.unix ))

Seeing this now, #728. I guess the examples have to be updated?

The examples did have to be updated, but #728 is unrelated.

So the error saying switch =flag`. I was looking for a package I had to install. Thanks for the detailed and didactic response, sir.

Running the code.ml with ocamlfind works as you suggested.

Running it with dune exec ./code throws this easily corrected error.

~/Downloads/lwt_examples-code
❯ dune exec ./code.exe                                                       
File "code.ml", line 4, characters 37-46:
4 |        let file = let open Lwt_io in open_file Input "code.ml" in
                                         ^^^^^^^^^
Error (warning 6): label mode was omitted in the application of this function.

Which is resolved by add the mode flag:

let () =
  Lwt_main.run begin
    let%lwt file = Lwt_io.(open_file ~mode:Input "code.ml") in
    begin
      let%lwt content = Lwt_io.read file in
      Lwt_io.print content
    end
    [%lwt.finally
      Lwt_io.close file]
  end

Seems like running with one build system is more strict than the other. Is this something to be expected?

Thanks, again.

Best.

That is a bug in the example, which I will fix soon.

Seems like running with one build system is more strict than the other. Is this something to be expected?

Yes, Dune passes more warning flags to the compiler, and, by default (i.e., when not building a release), turns most or all of them into errors. You can see exactly what flags it passes by calling dune with --verbose.

Nice. Thank you, sir.

Was this page helpful?
0 / 5 - 0 ratings