package ppx_defer

  1. Overview
  2. Docs
Go-like [%defer later]; now syntax

Install

Dune Dependency

Authors

Maintainers

Sources

ppx_defer-v0.4.0.tbz
sha256=c505b537074c98b0e33101ec8862fb3cf4538f16f94b89e67f51b810131ddde5
sha512=f8093c92c4033cdc97816dd0cd75fd0415db53aadce5be079a90e5ce91e2a30310024eb58f185bda4c2819fd7a2fe8c4426a2a0e2d79da5d7f9e2d16535e8e8e

Description

Published: 05 May 2020

README

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

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.

Dependencies (4)

  1. ppx_tools_versioned
  2. ocaml-migrate-parsetree >= "1.5.0" & < "2.0.0"
  3. dune >= "2.0"
  4. ocaml >= "4.02.3"

Dev Dependencies (2)

  1. lwt_ppx with-test
  2. lwt with-test

Used by

None

Conflicts

None