Lwt: Include Let_syntax module for ppx_let

Created on 21 Feb 2019  路  5Comments  路  Source: ocsigen/lwt

I'm trying to convert some code from Async to Lwt and it would be convenient if I could easily keep using ppx_let. ppx_let is nice because it works with other monad modules, and has syntax for both map and bind.

I could add the module myself but it gets messy.

I think all you need to do is add this module somewhere to Lwt.Infix:

module Let_syntax = struct
  let return = Lwt.return
  let map t ~f = Lwt.map f t
  let bind t ~f = Lwt.bind t f
  let both a b =
    bind a ~f:(fun a ->
      map b ~f:(fun b ->
        a, b))

  module Open_on_rhs = Lwt.Infix
end

(Ideally Lwt.both would exist too and then this could just reference it)

Most helpful comment

Note that the need for ppx_let will be greatly reduced starting next version, with let operators.

On the other hand, with let operators, we will implement something that look exactly like that module anyway, so ...

All 5 comments

This seems like a good idea.

If we add this to Lwt.Infix, is it correct that it will be used as:

open Lwt.Infix

(* ... *)

let%bind x = foo in (* ... *)

Since Lwt.Infix will be already open to bring Lwt.Infix.Let_syntax into scope, do we still need the line

module Open_on_rhs = Lwt.Infix

?

Note that the need for ppx_let will be greatly reduced starting next version, with let operators.

On the other hand, with let operators, we will implement something that look exactly like that module anyway, so ...

Yeah, it should make ppx_let just automatically work if Lwt.Infix is open.

I think you're right that Open_on_rhs isn't necessary. I'm slightly confused about what that piece of the definition is for, but I think you're fine without it.

@brendanlong would you be willing to make a quick PR for this that suits your needs? I'll be happy to review, test, and merge it.

@aantron I created #668 for this. Let me know if I didn't approach this right or something.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ljw1004 picture ljw1004  路  4Comments

aantron picture aantron  路  8Comments

aantron picture aantron  路  5Comments

ibukanov picture ibukanov  路  9Comments

aantron picture aantron  路  9Comments