[Rd] anonymous function parsing bug?
Wilm Schumacher
wilm.schumacher at gmail.com
Fri Oct 21 15:10:08 CEST 2016
Hi,
I hope this is the correct list for my question. I found a wired
behaviour of my R installation on the evaluation of anonymous functions.
minimal working example
###
f<-function(x) {
print( 2*x )
}(2)
class(f)
f(3)
f<-function(x) {
print( 2*x )
}(4)(5)
f(6)
###
leads to
###
> f<-function(x) {
+ print( 2*x )
+ }(2)
>
> class(f)
[1] "function"
>
> f(3)
[1] 6
Error in f(3) : attempt to apply non-function
>
> f<-function(x) {
+ print( 2*x )
+ }(4)(5)
>
> f(6)
[1] 12
Error in f(6) : attempt to apply non-function
###
is this a bug or desired behavior? Using parenthesis of coures solves
the problem. However, I think the operator precedence could be the
problem here. I looked at the "./src/main/gram.y" and I think that the
line 385
| FUNCTION '(' formlist ')' cr expr_or_assign %prec LOW
should be of way higher precedence. But I cannot forsee the side effects
of that (which could be horrible in that case).
If this is the desired behaviour and not a bug, I'm very interested in
the rational behind that.
Best wishes,
Wilm
ps:
$ R --version
R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
More information about the R-devel
mailing list