Skip to content

OCaml extension for a Go-ish [%defer ...] syntax

License

Notifications You must be signed in to change notification settings

hcarty/ppx_defer

Repository files navigation

ppx_defer - Go-like [%defer later]; now syntax.

Build status

This is an OCaml language extension implementing a somewhat Go-ish [%defer expr1]; expr2 which will defer the evaluation of expr1 until after expr2. expr1 will still be evaluated if expr2 raises an exception.

If you are using Lwt you can use [%defer.lwt expr1]; expr2.

Thanks to Drup for guidance in figuring out ppx details!

Using ppx_defer

As a simple example this code

let () =
  [%defer print_endline "world"];
  print_endline "Hello"

will print

Hello
world

as print_endline "world" was deferred until after print_endline "Hello".

A more common use case would be closing an external resource at the end of the current expression.

let () =
  let ic = open_in_bin "some_file" in
  [%defer close_in ic];
  let length = in_channel_length ic in
  let bytes = really_input_string ic length in
  print_endline bytes

This will close_in ic at the end of the current expression, even if an exception is raised.

See the examples/ directory for more examples.