Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use Lwt_unix with OCaml 5 effects? #1003

Open
aantron opened this issue Nov 12, 2023 · 3 comments
Open

How to use Lwt_unix with OCaml 5 effects? #1003

aantron opened this issue Nov 12, 2023 · 3 comments

Comments

@aantron
Copy link
Collaborator

aantron commented Nov 12, 2023

The following code is a minimized version of aantron/dream#297 that no longer calls into Dream at all:

type _ Effect.t += E : unit Effect.t

let () =
  Effect.Deep.try_with
    begin fun () ->
      Lwt_main.run begin
        Lwt.bind (Lwt_unix.sleep 1.) @@ fun () ->
        Effect.perform E;
        assert false
      end
    end
    ()
    {
      effc = fun (type a) (e : a Effect.t) ->
        match e with
        | E ->
          Option.some @@ fun (k : (a, _) Effect.Deep.continuation) ->
            prerr_endline "handling E";
            Effect.Deep.continue k ()
        | _ -> None
    }

Running this on OCaml 5.1.0 with Lwt 5.7.0 results in

Fatal error: exception Stdlib.Effect.Unhandled(Dune__exe__Json.E)

Replacing Lwt_unix.sleep 1. by, say, Lwt.pause () results in

handling E                           
Fatal error: exception File "dream/example/e-json/json.ml", line 9, characters 8-14: Assertion failed

which is the correct output, and what I would expect in both cases.

@raphael-proust
Copy link
Collaborator

@aantron
Copy link
Collaborator Author

aantron commented Nov 20, 2023

I looked at the code linked in #961 and the comments, but I don't see why an outer effect handler would not work, and the comments in the examples suggest that it still should. So I'm surprised by the behavior of this particular example.

@aantron
Copy link
Collaborator Author

aantron commented Nov 20, 2023

cc @ul

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants