Skip to main content

Posts

Showing posts with the label OCaml

OCaml 5 Concurrency: Migrating Legacy Lwt Code to Eio and Effect Handlers

  For over a decade, OCaml developers have relied on Lwt (Lightweight Threads) to handle concurrency. While Lwt successfully brought non-blocking I/O to a language without native support, it introduced "function coloring." Every function touching I/O had to return   'a Lwt.t , forcing codebases into chains of monadic binds ( >>= ) and intricate exception handling that obscured the business logic. With OCaml 5, we have Multicore and, more importantly for I/O,  Effect Handlers . These allow for direct-style concurrency. We no longer need the Promise monad to represent a suspended computation; the runtime stack itself can be suspended and resumed. This post dissects the migration of a TCP echo server from Lwt to  Eio  (Effects-based I/O), demonstrating how direct style simplifies control flow and how Structured Concurrency ( Eio.Switch ) prevents resource leaks. The Architecture Gap: Monads vs. Delimited Continuations To migrate effectively, you must understa...