Defer keyword
- Dominant language
- Rust
- Stars
- 145
- Forks
- 5
- PR merge metrics
- No merged PRs in 30d
Description
R has the `on.exit` construction for delaying execution of code, but I feel like this capability should be more central to a language. Taking inspiration from the `zig` language, providing a more native `defer` syntax would help to keep cleanup more localized.
To also take a nod from `withr` and `rust`'s lifetimes, I think `defer` should also act as a function that accepts a frame, allowing a function to defer execution until a specific frame completes execution. This is a bit like `python`'s context managers, but with the added benefit that they can be attached to arbitrary scopes and do not rely on implementing object-specific entry and exit methods.
```r
n_lines <- function(file) {
f <- open(file)
defer close(f)
length(read_lines(f))
}
```
```r
connect <- function(file) {
f <- open(file)
defer (parent_frame(-1)) {
close(f)
}
f
}
```
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.