I’m pleased to announce the release of two small compatibility libraries for modules added in OCaml 4.12.0:
-
either
– compatibility shims for theEither
module, which provides a canonical sum type in the standard library + various utility functions for it. To use it: simply addeither
as a library dependency and refer to theEither
module, which will either alias theEither
provided by the standard library or re-implement it as appropriate. -
semaphore-compat
– compatibility shims for theSemaphore
module insysthreads
, which provides binary and counting semaphores for use in multi-threaded programs. To use it: addsemaphore-compat
as a library dependency andopen Semaphore_compat
in any module that requires semaphores.Note on OCaml concurrency primitives. Users of OCaml’s
Mutex
module should beware that OCaml 4.12 features a changes to mutex semantics on certain platforms, since the mutexes are now “error checking” (as noted in the changelog). One consequence of this is that unlocking a mutex from a non-owning thread will now always fail, whereas previously this might succeed depending on the platform’s mutex implementation – notably,glibc
's mutexes allow this. If your code relies on this property of pre-4.12 mutexes, you may wish to add a dependency onsemaphore-compat
and switch to using binary semaphores instead (as these provide the right flavour of concurrency primitive in a POSIX-compatible manner).
Opam library authors making use of these compatibility libraries in their public API are encouraged to conditionally depend on them in order to ensure that downstream users of your library don’t pull in unnecessary compatibility shims. This can be done as follows:
depends: [
("ocaml" {>= "4.12.0"} | "either")
]
I hope you find these libraries useful!