switch_lang {rlang}R Documentation

Dispatch on call type

Description

Experimental lifecycle Questioning lifecycle

switch_lang() dispatches clauses based on the subtype of call, as determined by lang_type_of(). The subtypes are based on the type of call head (see details).

Usage

switch_lang(.x, ...)

coerce_lang(.x, .to, ...)

lang_type_of(x)

Arguments

.x, x

A language object (a call). If a formula quote, the RHS is extracted first.

...

Named clauses. The names should be types as returned by lang_type_of().

.to

This is useful when you switchpatch within a coercing function. If supplied, this should be a string indicating the target type. A catch-all clause is then added to signal an error stating the conversion failure. This type is prettified unless .to inherits from the S3 class "AsIs" (see base::I()).

Details

Calls (objects of type language) do not necessarily call a named function. They can also call an anonymous function or the result of some other expression. The language subtypes are organised around the kind of object being called:

The reason we use the term node head is because calls are structured as tree objects. This makes sense because the best representation for language code is a tree whose hierarchy is determined by the order of operations. See node for more on this.

Life cycle

All these functions are in the questioning stage and likely to be removed from the package.

Examples

# Named calls:
lang_type_of(~foo())

# Recursive calls:
lang_type_of(~foo$bar())
lang_type_of(~foo()())

# Namespaced calls:
lang_type_of(~base::list())

# For an inlined call, let's inline a function in the head node:
call <- quote(foo(letters))
call[[1]] <- base::toupper

call
lang_type_of(call)

[Package rlang version 0.3.4 Index]