Comments (9)
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 printf
s can get scheduled before the incr
.)
from lwt.
@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.
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.
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.
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.
This issue is present with the ppx syntax as well.
from lwt.
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.
@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.
@aantron That seems reasonable, thank you!
from lwt.
Related Issues (20)
- How to use Lwt_unix with OCaml 5 effects? HOT 3
- Exception printed in Lwt_stream.bounded_push#close documentation HOT 1
- Provide `Lwt.sleep` as a Unix-less function HOT 3
- Ppx_lwt docs link returns 404 error (since 5.4.0) HOT 1
- Drop support for OCaml < 4.08.0 HOT 4
- `src/unix/config/discover.exe` puts too much `-I` includes which can mislead the compiler
- EBADF from Lwt_process.with_* HOT 4
- Support `Lwt_process` in multi-domain settings HOT 1
- passing `env` in `Lwt_process` doesn't work on Windows HOT 1
- forking breaks lwt-io if happens after Lwt_main.run HOT 3
- memory consumption blowups in downstream projects using Lwt HOT 5
- Ocaml 5 build issue HOT 3
- Default Unix pool size (1000) is insane and dangerous
- SIGSEGV on OCaml 5 due to missing SA_ONSTACK HOT 2
- Clarify documentation of Lwt_stream.get_available
- Have a separate set of `Lwt_process.pread` that also return the process status HOT 2
- Documentation of `Lwt_stream.create` and `Lwt_stream.get`
- Need a way to share SIGCHLD with other libraries HOT 2
- Race in worker_loop HOT 1
- Library not found for `-llwt_unix_stubs` on macOS HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from lwt.