Skip to content

Commit

Permalink
Use minimal defer() instead of local_bindings()
Browse files Browse the repository at this point in the history
Fixes #1089
  • Loading branch information
hadley committed Jul 27, 2023
1 parent 5efaa34 commit 8640ad7
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions R/coerce.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,18 @@ deprecate_to_char <- function(type) {
)
}


# Can rewrite after https://github.com/r-lib/rlang/issues/1643
local_deprecation_user_env <- function(user_env = caller_env(2), frame = caller_env()) {
local_bindings(
deprecation_user_env = user_env,
.env = the,
.frame = frame
)

old <- the$deprecation_user_env
the$deprecation_user_env <- user_env
defer(the$deprecation_user_env <- old, frame)
}

# Lightweight equivalent of withr::defer()
defer <- function(expr, env = caller_env(), after = FALSE) {
thunk <- as.call(list(function() expr))
do.call(on.exit, list(thunk, TRUE, after), envir = env)
}

0 comments on commit 8640ad7

Please sign in to comment.