rlang icon indicating copy to clipboard operation
rlang copied to clipboard

is_expression() doesn't look at attributes of calls

Open moodymudskipper opened this issue 3 years ago • 2 comments

From ?rlang::is_expression:

In rlang, an expression is the return type of parse_expr(), the set of objects that can be obtained from parsing R code

x <- quote(a(1)(2))
attr(x[[1]], "foo") <- "bar"

# I'm ok with this
rlang::expr_deparse(x)
#> [1] "a(1)(2)"

# But then doesn't this mean this should be FALSE ?
rlang::is_expression(x)
#> [1] TRUE

Created on 2022-09-02 by the reprex package (v2.0.1)

For context in S4 objects we can find calls with attributes.

moodymudskipper avatar Sep 02 '22 20:09 moodymudskipper

That makes sense. But this might be a bit tricky, need to be careful about srcref attributes created by the parser on some language objects, e.g. {.

lionel- avatar Sep 03 '22 06:09 lionel-

Ah indeed good catch, I believe this implies that using current definition quote({a}) is not an expression either. Maybe we could have is_expression(x, ignore_attr = TRUE, ignore_srcref = TRUE) , these are arguments we find in waldo::compare().

moodymudskipper avatar Sep 03 '22 09:09 moodymudskipper

Maybe we could have is_expression(x, ignore_attr = TRUE, ignore_srcref = TRUE) , these are arguments we find in waldo::compare().

I'm not looking into extending the feature set of is_expression() further. I now regret having added it to the package because it doesn't seem worth the maintenance effort.

lionel- avatar Feb 16 '23 12:02 lionel-