-
Notifications
You must be signed in to change notification settings - Fork 14
feat: per-step setup/teardown with setup() #70
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
Open
schloerke
wants to merge
32
commits into
r-lib:main
Choose a base branch
from
schloerke:issue-68
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 14 commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
6ecda38
feat(setup): add exported setup() stub (#68)
schloerke e27617b
feat(setup): compile and run per-step setup/teardown (#68)
schloerke a55bf6b
docs(setup): explain step-teardown defer ordering (#68)
schloerke 4475d0d
test(setup): cover async await() teardown (#68)
schloerke 0d56af6
test(setup): cover stacked setups and reverse teardown order (#68)
schloerke 5fd85e1
test(setup): cover teardown isolation (#68)
schloerke 88d1c35
test(setup): cover compile-time guards (#68)
schloerke 0199b25
test(setup): pin known shortcomings with co-located tests (#68)
schloerke 3f351c3
test(setup): snapshot compiled state machine (#68)
schloerke dacd93a
test(setup): use env (not complex-LHS `<<-`) in await reprex (#68)
schloerke af9e33c
test(setup): clarify compile timing; make loop test discriminate per-…
schloerke 7959add
docs(setup): examples and NEWS for setup() (#68)
schloerke 276cfed
fix(setup): declare runtime helpers to satisfy R CMD check (#68)
schloerke 05334e6
docs(setup): add setup() to pkgdown reference index (#68)
schloerke 144e847
refactor(setup): drop withr dependency; teardowns eval in setup body …
schloerke cf5c238
docs(setup): condense defer-ordering comment per review (#68)
schloerke 169340c
test(setup): add withr::defer/local_* contract test (#68)
schloerke b3001f8
refactor(setup): extract setup lifecycle into init_setup_runtime() (#68)
schloerke ee10e0b
Merge remote-tracking branch 'origin/main' into issue-68
schloerke 973ab07
refactor(setup): rename teardown loop variables per review (#68)
schloerke 7bd6408
feat(setup): error when setup() is used in a loop (#68)
schloerke b0113b9
docs(setup): make the loop workaround a concrete runnable example (#68)
schloerke dd7bb9b
docs(setup): add second yield to workaround example to show per-step …
schloerke 61e0616
docs(setup): move loop usage into a dedicated help section (#68)
schloerke df2851e
docs(setup): use withr::defer() in examples instead of on.exit() (#68)
schloerke ed8819f
docs(setup): describe setup scope as a child environment, not "isolat…
schloerke 2f9b597
docs(setup): add 'Assignments and scope' help section (#68)
schloerke b7e122b
docs(setup): add withr::local_options() per-step hygiene example (#68)
schloerke a6f269e
docs(setup): fix and simplify loop-section example (#68)
schloerke c413ce6
docs(setup): expand help with lifecycle, scope, and loop sections (#68)
schloerke 4ef2f54
test(setup): reframe expected-behavior tests, drop 'known limitation'…
schloerke 5881654
test(setup): cover setup() in lapply/replicate (errors via stub) (#68)
schloerke File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,5 +21,6 @@ export(gen) | |
| export(generator) | ||
| export(is_exhausted) | ||
| export(loop) | ||
| export(setup) | ||
| export(yield) | ||
| import(rlang) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| #' Set up per-step state in a coroutine | ||
| #' | ||
| #' @description | ||
| #' `setup()` registers an expression that runs at the start of **every** step of | ||
| #' a [generator()] or [async()] function: the initial entry and every resumption | ||
| #' after a [yield()] or [await()]. Any `withr::defer()`, `withr::local_*()`, or | ||
| #' [on.exit()] registered while `expr` runs is torn down at the **end of that | ||
| #' step** (the next `yield`/`await`, a `return`, normal completion, an error, or | ||
| #' early close). | ||
| #' | ||
| #' This gives setup/teardown parity for each step of a coroutine, unlike a | ||
| #' top-level `on.exit()` which only fires once when the whole function exits. | ||
| #' | ||
| #' @details | ||
| #' - `expr` runs in an isolated environment: it can read the function's | ||
|
schloerke marked this conversation as resolved.
Outdated
|
||
| #' arguments, locals, and lexical scope, and mutate external state | ||
| #' (`the$x <- 1`, `<<-`), but **plain assignments stay local to `expr`** and are | ||
| #' not visible to the function body. | ||
| #' - When execution reaches a `setup()` call it is registered and runs for the | ||
| #' current step; re-encountering the *same* `setup()` call (e.g. on a later loop | ||
|
schloerke marked this conversation as resolved.
Outdated
|
||
| #' iteration) is a no-op. A `setup()` inside an `if`/loop registers only if and | ||
| #' when reached. | ||
| #' - Multiple `setup()` calls stack and run in registration order each step; their | ||
| #' teardowns fire in reverse order. | ||
| #' - `setup()` cannot contain `yield()`/`await()` and its result cannot be | ||
| #' assigned. | ||
| #' | ||
| #' Like [yield()] and [await()], `setup()` is a syntactic construct recognised by | ||
| #' the coroutine compiler. Calling it directly (outside a coroutine body) is an | ||
| #' error. | ||
| #' | ||
| #' @param expr An expression to run at the start of each step. | ||
| #' | ||
| #' @seealso [generator()], [async()], [yield()], [await()]. | ||
| #' @examples | ||
|
schloerke marked this conversation as resolved.
|
||
| #' the <- new.env() | ||
| #' the$x <- 0 | ||
| #' | ||
| #' gen <- generator(function() { | ||
| #' setup({ | ||
| #' old_x <- the$x | ||
| #' the$x <- 1 | ||
| #' withr::defer(the$x <- old_x) | ||
| #' }) | ||
| #' yield(the$x) # 1 while the step runs | ||
| #' yield(the$x) # 1 again: setup re-ran for this step | ||
| #' }) | ||
| #' | ||
| #' g <- gen() | ||
| #' g() # 1 | ||
| #' the$x # 0 — restored at the end of the step | ||
| #' g() # 1 | ||
| #' the$x # 0 | ||
| #' @export | ||
| setup <- function(expr) { | ||
| abort("`setup()` can't be called directly or within function arguments.") | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,7 @@ reference: | |
| - generator | ||
| - gen | ||
| - yield | ||
| - setup | ||
| - loop | ||
| - collect | ||
|
|
||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| # setup() compiles to a do_setup() state | ||
|
|
||
| Code | ||
| generator_body(function() { | ||
| setup({ | ||
| old <- the$x | ||
| withr::defer(the$x <- old) | ||
| }) | ||
| yield(1) | ||
| }) | ||
| Output | ||
| { | ||
| if (exhausted) { | ||
| return(invisible(exhausted())) | ||
| } | ||
| repeat { | ||
| switch(state[[1L]], `1` = { | ||
| do_setup(1L, quote({ | ||
| old <- the$x | ||
| withr::defer(the$x <- old) | ||
| })) | ||
| state[[1L]] <- 2L | ||
| }, `2` = { | ||
| user({ | ||
| 1 | ||
| }) | ||
| state[[1L]] <- 3L | ||
| suspend() | ||
| return(last_value()) | ||
| }, `3` = { | ||
| .last_value <- if (missing(arg)) exhausted() else arg | ||
| state[[1L]] <- 4L | ||
| }, `4` = { | ||
| exhausted <- TRUE | ||
| return(exhausted()) | ||
| }) | ||
| } | ||
| exhausted <- TRUE | ||
| invisible(exhausted()) | ||
| } | ||
|
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.