Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add protect function, as alternative to missing try finally construct #155

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### API changes

- Add `Math.Int.floor` and `Math.Int.random`, https://github.com/rescript-association/rescript-core/pull/156
- Add `protect` https://github.com/rescript-association/rescript-core/pull/155

### Documentation

Expand Down
26 changes: 26 additions & 0 deletions src/RescriptCore.res
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,29 @@ type undefined<+'a> = Js.undefined<'a>
type nullable<+'a> = Js.nullable<'a>

let panic = Core__Error.panic

/**
`protect(~finally, f)`

Tries to execute the function `f`, and ensures that `finally` will be called
whether `f` raises an exception or not.

Any exception raised by `f` will be re-raised in order to be handled by the
user. If `finally` raises, then that exception will be emitted instead, and
any exception raised by `f` will go unnoticed.

```res example
try protect(~finally=() => Js.log("finally"), () => failwith("oh no!")) catch {
| Failure(err) => Js.log(err)
}
```
Comment on lines +73 to +86
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Glenn, just a little formatting

Suggested change
`protect(~finally, f)`
Tries to execute the function `f`, and ensures that `finally` will be called
whether `f` raises an exception or not.
Any exception raised by `f` will be re-raised in order to be handled by the
user. If `finally` raises, then that exception will be emitted instead, and
any exception raised by `f` will go unnoticed.
```res example
try protect(~finally=() => Js.log("finally"), () => failwith("oh no!")) catch {
| Failure(err) => Js.log(err)
}
```
`protect(~finally, f)` tries to execute the function `f`, and ensures that
`finally` will be called whether `f` raises an exception or not.
Any exception raised by `f` will be re-raised in order to be handled by the
user. If `finally` raises, then that exception will be emitted instead, and
any exception raised by `f` will go unnoticed.
## Examples
```rescript
try protect(~finally=() => Console.log("finally"), () => panic("oh no!")) catch {
| Failure(err) => Console.log(err)
}

*/
let protect = (~finally, f) => {
let result = try f() catch {
| exn =>
finally()
raise(exn)
glennsl marked this conversation as resolved.
Show resolved Hide resolved
}
finally()
result
}