Skip to content

How to use Lwt_unix with OCaml 5 effects? #1003

@aantron

Description

@aantron

The following code is a minimized version of camlworks/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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions