OCaml 5


Effect handlers

No primitives for concurrency till Ocaml5.

Can think of effect handlers as 'first-class, restartable exceptions'

(* When [E] is performed, we will get a value of type [string] *)
effect E: string

(* A computation. Like a thunk *)
let comp () = 
  print_string "0: start";
  print_string (perform E);
  print_string "3: end";

let main () = 
  try
    comp ()
  (* Effect handler. [k] is a delimited continuation *)
  with effect E k ->
    print_string "1: EH start";
    continue k "2: EH cont";
    print_string "4: EH end";

Here control goes like: