Coder Social home page Coder Social logo

Comments (9)

rneswold avatar rneswold commented on May 25, 2024

lwt.syntax is correct because you're mixing imperative code with monadic code. To get rid of the warning, you could do either

lwt () =
  let i = ref 0 in
  Lwt.ignore_result (Lwt_io.printlf "Hello %d" !i >> incr i);
  Lwt_io.printlf "Hello %d" !i

or

lwt () =
  let i = ref 0 in
  Lwt_io.printlf "Hello %d" !i >>
  Lwt.return @@ incr i >>
  Lwt_io.printlf "Hello %d" !i

I like the second solution since it's fully monadic and forces the ordering (the first solution, I think, has a potential race condition where the two printfs can get scheduled before the incr.)

from lwt.

rneswold avatar rneswold commented on May 25, 2024

@hcarty (or @diml): I believe this issue can be closed. My previous comment explains why the error message is correct and how the submitter can fix his code.

from lwt.

hcarty avatar hcarty commented on May 25, 2024

This should probably be documented. It's a bit surprising for a newcomer to the library/style as it prevents converting:

Lwt_io.printlf "Hello %d" !i >>= fun () ->
foo ();
Lwt_io.printlf "Hello %d" !i >>= fun () ->
...

to use the syntax extension (or the ppx version of the syntax if it acts the same way). One needs to touch non-Lwt code when going back and forth between vanilla Lwt and pa_lwt code.

from lwt.

rneswold avatar rneswold commented on May 25, 2024

The sequence operator, ;, isn't handled by strict rules, as it probably should. How does it work for you when you clarify the sequence using begin/end? For instance, is your example supposed to be this:

(Lwt_io.printlf "Hello %d" !i >>= fun () -> foo ());
(Lwt_io.printlf "Hello %d" !i >>= fun () ->  ...)

or this:

Lwt_io.printlf "Hello %d" !i >>=
  fun () ->
    begin
      foo ();
      Lwt_io.printlf "Hello %d" !i
    end >>=
  fun () ->  ...

The former should give you a warning because the first line presumably returns 'a Lwt.t when it should return unit. In the second example, the print statements and the call to foo are handled in order.

I don't know which has higher precedence, >>= or ;, so I'm not sure how your example should be parsed.

I've been getting in the habit of using begin/end (or sometimes parentheses) around my imperative code because it forces a certain interpretation of a sequence of commands (like parentheses do in expressions) and it makes it more obvious in the code that it's an imperative section.

from lwt.

hcarty avatar hcarty commented on May 25, 2024

I would expect this:

let i = ref 0 in
Lwt_io.printlf "Hello %d" !i >>
incr i;
Lwt_io.printlf "Hello %d" !i

and this:

let i = ref 0 in
Lwt_io.printlf "Hello %d" !i >>= fun () ->
incr i;
Lwt_io.printlf "Hello %d" !i

to be equivalent, both also being equivalent to:

let i = ref 0 in
Lwt_io.printlf "Hello %d" !i >>= fun () -> (
  incr i;
  Lwt_io.printlf "Hello %d" !i
)

In this case I expect it is fun vs ; rather than >>= vs ; which ultimately determine the actual interpretation of the code.

from lwt.

hcarty avatar hcarty commented on May 25, 2024

This issue is present with the ppx syntax as well.

from lwt.

Drup avatar Drup commented on May 25, 2024

I'm not sure we can do much about it without introducing brittle complexity in the syntax extensions. We can't change the respective priority of >>, fun and ;, so we would need to basically inspect the AST and reorder nodes. It's a bad idea, since we don't have access to parens.

from lwt.

aantron avatar aantron commented on May 25, 2024

@hcarty I propose only adding a note to the documentation – see above commit. It doesn't seem like there is an alternative to >> either, since ; is the infix operator with the lowest precedence.

from lwt.

hcarty avatar hcarty commented on May 25, 2024

@aantron That seems reasonable, thank you!

from lwt.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.