Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/rizo/optimist

Everything is going to be Ok.
https://github.com/rizo/optimist

Last synced: about 1 month ago
JSON representation

Everything is going to be Ok.

Awesome Lists containing this project

README

        

# Optimist

An OCaml ppx that helps you write happier code.

## Supported rewrite rules

```ocaml
let%ok ... = ... in ...
let%some ... = ... in ...
let%await ... = ... in ...
let%await.ok ... = ... in ...
let%await.some ... = ... in ...

match%ok ... with ...
match%some ... with ...
match%await ... with ...
match%await.ok ... with ...
match%await.some ... with ...

if%ok ... then ... else ...
if%some ... then ... else ...
if%await ... then ... else ...
if%await.ok ... then ... else ...
if%await.some ... then ... else ...
```

## Lwt backtrace propagation

The main difference between using `(let*)` and `let%await` (or the standard
`let%lwt`) is that, with the ppx version, the async exceptions will correctly
propagate their backtraces.

The following example demonstrates the fully expanded code for `%await.ok`

```ocaml
let%await.ok p = m in e

(* Is desugared into... *)

let module Reraise = struct
external reraise : exn -> 'a = "%reraise"
end
in
Lwt.backtrace_bind
(fun exn -> try Reraise.reraise exn with exn -> exn)
m
(fun p_result ->
match with
| Ok p -> e
| Error err -> Lwt.return (Error err))
```