[Rd] ?Syntax wrong about `?`'s precedence ?
Ant F
@nto|ne@|@br| @end|ng |rom gm@||@com
Thu Aug 29 13:05:59 CEST 2019
Dear all,
`?Syntax` documents that `?` has the lowest precedence, right under `=`.
Indeed it reads:
*The following unary and binary operators are defined. They are listed in
precedence groups, from highest to lowest. *
and ends the list with
*<- <<-* *assignment (right to left)*
*=* *assignment (right to left)*
*?* *help (unary and binary)*
I believe it to be wrong, `=` has lower precedence than `?`.
See the following example :
`?` <- `+`
x = 2 ? 3
x
#> [1] 5
We see that `2 ? 3` is evaluated first, then the result is assigned to x,
showing
higher precedence for `?`.
Compare it to the similar code using `<-` :
x <- 2 ? 3
#> [1] 5
x
#> [1] 2
Here first `x <- 2` is evaluated, then its output is added to 3, and the
result
`5` is printed. and we verify that `x` is still `2`. Showing lower
precedence
for `?` consistent with the doc.
Hadley Wickham's package `lobstr` makes it easy to compare the parse trees:
lobstr::ast({x = 2 ? 3})
#> o-`{`
#> \-o-`=`
#> +-x
#> \-o-`?`
#> +-2
#> \-3
lobstr::ast({x <- 2 ? 3})
#> o-`{`
#> \-o-`?`
#> +-o-`<-`
#> | +-x
#> | \-2
#> \-3
Best regards,
Antoine
[[alternative HTML version deleted]]
More information about the R-devel
mailing list