package ppx_defer

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

Install

Dune Dependency

Authors

Maintainers

Sources

ppx_defer-v0.5.0.tbz
sha256=802092d24925eb163663567c7c1d0bdf1e9d0a7e6a58b0854b78fa24a19df05c
sha512=dabf65b2674a6780677bfb56c501992a4f495c41343e47f2bab57fe533f06f68adca0d9d48df4b26b7391cf5fee544498c2641653ddbc401f2f76c28e0b951a9

Description

Published: 10 Sep 2021

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 (3)

  1. ppxlib >= "0.12.0"
  2. dune >= "2.0"
  3. ocaml >= "4.11.0"

Dev Dependencies (2)

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

Used by

None

Conflicts

None

OCaml

Innovation. Community. Security.