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())
A value created using constructors.
A constructor pattern to test expr
against.
The environment where constructors can be found.
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.
NULL
if the pattern does not match or an environment with
bound variables if it does.
test_pattern_
: Version that quotes test_expr
itself.
test_pattern
: Version that quotes test_expr
itself.
# NOT RUN {
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
test_pattern_(one, quote(ZERO)) # returns NULL
as.list(test_pattern_(one, quote(ONE(v)))) # returns a binding for v
as.list(test_pattern(two, TWO(v,w))) # returns a binding for v and w
# }
Run the code above in your browser using DataLab