Re: [Rd] delayedAssign changing values

From: Simon Urbanek <simon.urbanek_at_r-project.org>
Date: Thu, 26 Apr 2012 11:26:01 -0400

On Apr 25, 2012, at 5:18 PM, McGehee, Robert wrote:

> I'm not sure if this is a known peculiarity or a bug, but I stumbled across what I think is very odd behavior from delayedAssign. In the below example x switches values the first two times it is evaluated.
>

>> delayedAssign("x", {x <- 2; x+3})
>> x==x

> [1] FALSE
>
>> delayedAssign("x", {x <- 2; x+3})
>> x

> [1] 5
>> x

> [1] 2
>
> The ?delayedAssign documentation says that "after [evaluation], the value is fixed and the expression will not be evaluated again." However, this appears not to be true.

It is actually true, the value is not evaluated twice: your evaluation of "x" has modified the value of "x" (hence it is no longer a promise) so what you get in the next evaluation is the value that you have set as a side-effect of the promise evaluation. However, the forced value of the promise it still the value returned by the evaluated expression. YOu can see that it is the case by simply adding a function with side-effect (like cat) into your expression.

> Is this a bug, or just a good way to write extremely obfuscated code?
>

That is a good question and I don't know the answer. The docs don't say which value will be fixed so it could be either way, but intuitively I would expect the promise evaluation to override side-effects.

Cheers,
Simon



R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Thu 26 Apr 2012 - 15:28:38 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 26 Apr 2012 - 17:00:50 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive