Learn R Programming

rlang (version 0.1)

return_from: Jump to or from a frame

Description

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.

Usage

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.

Examples

Run this code
# NOT RUN {
# Passing fn() evaluation frame to g():
fn <- function() {
  val <- g(get_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()

# 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()
# }

Run the code above in your browser using DataLab