Skip to contents

assert_not() is the inverse of a single specification: it errors when x would be accepted by spec, and returns x unchanged otherwise.

Usage

assert_not(
  x,
  spec,
  ...,
  x_arg = caller_arg(x),
  call = caller_env(),
  x_class = object_type(x)
)

Arguments

x

The object to stabilize.

spec

A single stabilizer function, to_* function, specify_*() result, or assert_* function that x must not match.

...

Reserved for future use; must be empty.

x_arg

(character(1)) The name of the object being stabilized to use in error messages. The automatic value will work in most cases, or pass it through from higher-level functions to make error messages clearer in unexported functions.

call

(environment) The execution environment to mention as the source of error messages.

x_class

(character(1)) The class name of the object being stabilized to use in error messages. Use this if you remove a special class from the object before checking its coercion, but want the error message to match the original class.

Value

x, unchanged, if x does not match spec, or an error condition with classes <stbl-error>, <stbl-condition>, <rlang_error>, <error>, <condition>, and <stbl-error-matched_spec> when x matches spec.

Examples

assert_not("a", specify_int())
#> [1] "a"
assert_not(1.5, stabilize_int)
#> [1] 1.5

# Errors because "1" is int-ish, even though it isn't literally an integer
try(assert_not("1", specify_int()))
#> Error in eval(expr, envir) : 
#>   `"1"` must not match "specify_int()".

# Errors because stabilize_int() succeeds outright on 1L
try(assert_not(1L, stabilize_int))
#> Error in eval(expr, envir) : 
#>   `1L` must not match "stabilize_int".