https://github.com/hcarty/with_return
Early returns in OCaml including syntax sugar
https://github.com/hcarty/with_return
Last synced: 9 months ago
JSON representation
Early returns in OCaml including syntax sugar
- Host: GitHub
- URL: https://github.com/hcarty/with_return
- Owner: hcarty
- License: mit
- Created: 2017-12-13T00:00:19.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-03-22T17:27:47.000Z (about 8 years ago)
- Last Synced: 2025-04-03T22:23:04.152Z (about 1 year ago)
- Language: OCaml
- Size: 4.88 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# with\_return - Early return inside expressions
`with_return` is a tiny library and matching OCaml syntax extension
implementing an early return mechanism using exceptions.
Thanks to octachron for the initial code living in the `With_return` module.
## Using ppx\_return
The bundled ppx syntax extension makes using this functionality syntactically
lighter.
```ocaml
let add_one_if_negative x =
(* The name return is used by default *)
[%with_return];
if x >= 0 then return (Error "not negative");
Ok (x + 1)
```
The name of the value used to return early can be customized if `return` would shadow local names.
```ocaml
let add_one_if_negative x =
(* Now use ret instead of return *)
let myret = [%with_return] in
if x >= 0 then myret (Error "not negative");
Ok (x + 1)
```