Test if a value, expr, created from constructors matches a pattern of constructors. The test_pattern_ function requires that test_expr is a quoted expression while the test_pattern function expects a bare expression and will quote it itself.

test_pattern_(expr, test_expr, eval_env = rlang::caller_env(),
  match_parent_env = rlang::caller_env())

test_pattern(expr, test_expr, eval_env = rlang::caller_env(),
  match_parent_env = rlang::caller_env())

Arguments

expr

A value created using constructors.

test_expr

A constructor pattern to test expr against.

eval_env

The environment where constructors can be found.

match_parent_env

Environment to use as the parent of the match bindings we return. This parameter enables you provide additional values to the environment where match-expressions are evaluated.

Value

NULL if the pattern does not match or an environment with bound variables if it does.

Functions

  • test_pattern_: Version that quotes test_expr itself.

  • test_pattern: Version that quotes test_expr itself.

Examples

type := ZERO | ONE(x) | TWO(x,y) zero <- ZERO one <- ONE(1) two <- TWO(1,2) as.list(test_pattern(zero, ZERO)) # returns an empty binding
#> list()
test_pattern_(one, quote(ZERO)) # returns NULL
#> NULL
as.list(test_pattern_(one, quote(ONE(v)))) # returns a binding for v
#> $v #> [1] 1 #>
as.list(test_pattern(two, TWO(v,w))) # returns a binding for v and w
#> $v #> [1] 1 #> #> $w #> [1] 2 #>