[Rd] order of operations

Duncan Murdoch murdoch@dunc@n @end|ng |rom gm@||@com
Fri Aug 27 21:14:56 CEST 2021


On 27/08/2021 3:06 p.m., Enrico Schumann wrote:
> On Fri, 27 Aug 2021, Gabor Grothendieck writes:
> 
>> Are there any guarantees of whether x will equal 1 or 2 after this is run?
>>
>> (x <- 1) * (x <- 2)
>> ## [1] 2
>> x
>> ## [1] 2
> 
> At least the "R Language Definition" [1] says
> 
>    "The exponentiation operator ‘^’ and the left
>     assignment plus minus operators ‘<- - = <<-’
>     group right to left, all other operators group
>     left to right.  That is  [...]  1 - 1 - 1 is -1"
> 
> which would imply 2.

I think this is a different issue.  There's only one operator in 
question (the "*").  The question is whether x*y evaluates x first or y 
first (and I believe the answer is that there are no guarantees).  I'm 
fairly sure both are guaranteed to be evaluated, under the rules for 
group generics listed in ?groupGeneric, but I'm not certain the 
guarantee is honoured in all cases.

Duncan Murdoch



More information about the R-devel mailing list