Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/hcarty/ppx_defer
OCaml extension for a Go-ish [%defer ...] syntax
https://github.com/hcarty/ppx_defer
ocaml ocaml-extension ppx
Last synced: about 2 months ago
JSON representation
OCaml extension for a Go-ish [%defer ...] syntax
- Host: GitHub
- URL: https://github.com/hcarty/ppx_defer
- Owner: hcarty
- License: mit
- Created: 2016-02-10T16:08:51.000Z (almost 9 years ago)
- Default Branch: main
- Last Pushed: 2022-05-30T16:50:54.000Z (over 2 years ago)
- Last Synced: 2023-03-12T05:03:10.297Z (almost 2 years ago)
- Topics: ocaml, ocaml-extension, ppx
- Language: OCaml
- Size: 35.2 KB
- Stars: 17
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGES.md
- License: LICENSE.md
Awesome Lists containing this project
README
# ppx_defer - Go-like `[%defer later]; now` syntax.
![Build status](https://github.com/hcarty/ppx_defer/workflows/Build%20and%20test%20ppx_defer/badge.svg)
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
```ocaml
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.
```ocaml
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.