How to compile Let_syntax of Base?

I’m reading RWO v2 and try to write my own demo as:

open Base
open Stdio

let f1 n =
  if n < 10 then None else Some n

let f2 n =
  if n > 15 then None else Some n

let combines n =
  let open Option.Let_syntax in
  let%bind r1 = f1 n in
  let%bind r2 = f2 n in
  Some r2

let _ = 
  match combines 13 with
  | Some x -> print_int x
  | None -> print_endline "nothing"

The jbuild file:

 ((names (t))
  (libraries (base stdio))

But there’s an error:

$ jbuilder build t.exe
File "", line 12, characters 6-10:
Error: Uninterpreted extension 'bind'.

AFAIK you should have ppx_let installed (via opam install ppx_let if haven’t transitively depended), and add the preprocess stanza on your jbuild file, as such:

 ((names (t))
  (libraries (base stdio))
  (preprocess (pps (ppx_let)))

In general, the ppx syntax extensions used in RWO are all transitively included by ppx_jane, so using that in your jbuild file should fix it for [@@deriving sexp], [%compare: int list], etc.

1 Like

I seem to have almost the exact same issue but updated for 2020

some code:

let _ =
  let open Option.Let_syntax in
  let%bind x = Some(1) in

the dune:

 (name sre)
  (pps ppx_let ppx_inline_test))
 (libraries str))

the dune build output:

File "lib/", line 2, characters 11-28:
2 |   let open Option.Let_syntax in
Error: Unbound module Option.Let_syntax

Base is not listed in the dune file. Try replacing (libraries str) with (libraries base str)

1 Like

Adding base didn’t work, same error. Could something be missing from opam?

You’ll also probably need to open Base to the top of the .ml file so that you’ll be using the Base version of the option module.

1 Like

That was it, thanks @mseri & @Levi_Roth !