Learn R Programming

async (version 0.3.2)

goto: Coroutine switch with delimited goto.

Description

The switch function implemented for coroutines in the async package is more strict than the one in base R. In a coroutine, switch will always either take one of the given branches or throw an error, whereas base R switch will silently return NULL if no branch matches switch argument. Otherwise, the same conventions apply as base::switch() (e.g. empty switch branches fall through; a character switch may have one unnamed argument as a default.)

Usage

goto(branch = NULL)

Arguments

branch

A character string naming the new branch. If missing or NULL, jumps back to re-evaluate the switch argument.

Details

Coroutine switch also supports a delimited form of goto. Within a branch, goto("other_branch") will stop executing the present branch and jump to the named branch. Calling goto() without arguments will jump back to re-evaluate the switch expression.

If a goto appears in a try-finally call, as in:

switch("branch",
   branch=tryCatch({...; goto("otherBranch")},
                   finally={cleanup()}),
   otherBranch={...}
)

the finally clause will be executed before switching to the new branch.