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

Use defer to do some post-modification of parent return #276

Closed
shaman-yellow opened this issue Dec 28, 2024 · 1 comment
Closed

Use defer to do some post-modification of parent return #276

shaman-yellow opened this issue Dec 28, 2024 · 1 comment

Comments

@shaman-yellow
Copy link

The case as following, but get 11 instead of 1100. Is this possible?

inner <- function(x, env = parent.frame(1)) {
  withr::defer(assign("x", get("x", envir = env) * 100, envir = env), env)
}
outer <- function(x) {
  inner(x)
  x <- x + 1
  return(x)
}
outer(10)
@lionel-
Copy link
Member

lionel- commented Jan 6, 2025

I think it's expected. return() gets the value x eagerly and the on-exit expression runs after return() is called.

@lionel- lionel- closed this as completed Jan 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants