18.5 Constants

  • Scalar constants are the simplest component of the AST.
  • A constant is either NULL or a length-1 atomic vector (or scalar)
    • e.g., TRUE, 1L, 2.5 or x.
  • We can test for a constant with rlang::is_syntactic_literal().
  • Constants are self-quoting in the sense that the expression used to represent a constant is the same constant:
identical(expr(TRUE), TRUE)
#> [1] TRUE
# [1] TRUE
identical(expr(1), 1)
#> [1] TRUE
# [1] TRUE
identical(expr(2L), 2L)
#> [1] TRUE
# [1] TRUE
identical(expr("x"), "x")
#> [1] TRUE
# [1] TRUE