[Rd] Language definition question - order of argument side effects

Justin Talbot jtalbot at stanford.edu
Fri Sep 30 18:26:53 CEST 2011


I'm interested in the difference between these two intuitively
equivalent sequences that produce different results (in R version
2.13.1 (2011-07-08) 32-bit). I think R's reference counting
optimization is causing this difference in behavior.

> a <- 1
> a+{a[1] <- 20}
[1] 21

> a <- 1
> a[1] <- 1
> a+{a[1] <- 20}
[1] 40

Is one of these the "correct" answer, or is the order of side effects
undefined in these statements? Section 4.3.3 of the R Language
Definition just says that doing assignment in an argument to a
function is "bad style", but doesn't say anything about evaluation
order.

In general, for primitive and internal functions, is a particular
evaluation order for the arguments guaranteed?

Thanks,
Justin Talbot



More information about the R-devel mailing list