ocaml-multicore / eio

Effects-based direct-style IO for multicore OCaml
Other
540 stars 66 forks source link

README discussion of `type 'a env` convention is out of date #613

Closed jonsterling closed 1 year ago

jonsterling commented 1 year ago

I am trying to update my project to handle the new API changes (replacing objects with first class modules and variants), and I am having a little trouble understanding how to adapt the type 'a env convention described in the README, which is now outdated. I'm filing this ticket to put the README issue on your radar, but maybe this is also a good opportunity for me to learn how to use this properly.

The issue is that the release notes for 0.12 say that we can replace #foo with _ foo, but this does not work in type declarations. I ended up getting very confused about what it is I should put there, because I have to admit that I am not fully comfortable yet with OCaml's variants and how to control them, vis-a-vis subtyping, etc. What I ended up change my code to (which type-checked) was the following:

type 'a env = <
  cwd : Eio.Fs.dir_ty Eio.Path.t;
  process_mgr : [>] Eio.Process.mgr;
  stdout : [>] Eio.Flow.sink;
  ..
> as 'a

Is this what you intend, or is there a better way to write this? I didn't actually know what [>] means. If this is the right way to write this code, then I can update the Eio README in a PR. thanks for your help and patience!

talex5 commented 1 year ago

Yes, that's right. [>] is the same as _ here (since it has to be a polymorphic variant), except that OCaml doesn't allow _ here for some reason (Error: A type wildcard "_" is not allowed in this type declaration.).

In #614 I've used constraint instead, which perhaps avoids the confusion.

jonsterling commented 1 year ago

cool, thanks so much for your help @talex5! At some point I may also have some other feedback and questions about the new API, but maybe I should take those to the Discourse board. Anyway, thanks again :)