abu fahad
abu fahad

Reputation: 23

Troubleshooting Compilation Error with lwt_ppx in OCaml: Unable to Compile File

I'm encountering an issue while using lwt_ppx and the let%lwt syntax as described in the CS3110 textbook's section on promises (https://cs3110.github.io/textbook/chapters/ds/promises.html).

In the code snippet provided:

open Lwt_io

let p =
  let%lwt s1 = read_line stdin in
  let%lwt s2 = read_line stdin in
  Lwt_io.printf "%s\n" (s1^s2)

let _ = Lwt_main.run p

I'm trying to compile the code using dune. My dune file looks like this:

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

However, when I run the command dune exec ./main.exe, I'm getting the error message "Uninterpreted extension 'lwt'."

I've verified that I have the lwt_ppx package installed using opam list, and interestingly, the code works as expected in utop when I run:

#require "lwt_ppx";;
#require "lwt.unix";;
#use "main.ml";;

What might I be doing wrong with my dune configuration? Any help would be greatly appreciated.

Upvotes: 2

Views: 299

Answers (1)

glennsl
glennsl

Reputation: 29136

lwt_ppx is a preprocessor, and so also needs to be added to the preprocess stanza:

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

But also note that since 4.08 you can use let operators for much the same purpose, without any preprocessor. For example, you can bind Lwt.bind to let*, then your example can be written as:

open Lwt_io

let (let*) = Lwt.bind

let p =
  let* s1 = read_line stdin in
  let* s2 = read_line stdin in
  printf "%s\n" (s1^s2)

let _ = Lwt_main.run p

Upvotes: 3

Related Questions