Questioning lifecycle

While base::return() can only return from the current local frame, these two functions will return from any frame on the current evaluation stack, between the global and the currently active context. They provide a way of performing arbitrary non-local jumps out of the function currently under evaluation.

return_from(frame, value = NULL)

return_to(frame, value = NULL)

Arguments

frame

An environment, a frame object, or any object with an get_env() method. The environment should be an evaluation environment currently on the stack.

value

The return value.

Details

return_from() will jump out of frame. return_to() is a bit trickier. It will jump out of the frame located just before frame in the evaluation stack, so that control flow ends up in frame, at the location where the previous frame was called from.

These functions should only be used rarely. These sort of non-local gotos can be hard to reason about in casual code, though they can sometimes be useful. Also, consider to use the condition system to perform non-local jumps.

Life cycle

The support for frame object is soft-deprecated. Please pass simple environments to return_from() and return_to().

These functions are in the questioning lifecycle because we are considering simpler alternatives.

Examples

# Passing fn() evaluation frame to g():
fn <- function() {
  val <- g(current_env())
  cat("g returned:", val, "\n")
  "normal return"
}
g <- function(env) h(env)

# Here we return from fn() with a new return value:
h <- function(env) return_from(env, "early return")
fn()
#> [1] "early return"

# Here we return to fn(). The call stack unwinds until the last frame
# called by fn(), which is g() in that case.
h <- function(env) return_to(env, "early return")
fn()
#> g returned: early return 
#> [1] "normal return"