From lachmann at eva.mpg.de  Fri Apr 27 00:10:34 2012
From: lachmann at eva.mpg.de (ghostwheel)
Date: Thu, 26 Apr 2012 15:10:34 -0700 (PDT)
Subject: [Rd] delayedAssign changing values
In-Reply-To: <D0BEE478-C3F3-47BB-B468-AC0098B94433@r-project.org>
References: <17B09E7789D3104E8F5EEB0582A8D66FCD1EAC8145@MSGRTPCCRF2WIN.DMN1.FMR.COM>
	<ED11A19C-0066-4D2C-845C-C41747A94BF5@r-project.org>
	<1335455991422-4590242.post@n4.nabble.com>
	<D0BEE478-C3F3-47BB-B468-AC0098B94433@r-project.org>
Message-ID: <1335478234841-4591137.post@n4.nabble.com>


Simon Urbanek wrote
> 
>> More intuitive would have been the behavior
>> delayedAssign("x", local({y <- 7; y+3}) ) 
>> which only changes x.
> 
> That is questionable - I think it is more logical for both environments to
> be the same as default. Just think if it -- the point here is to access
> lazy evaluation which is exactly what it does - lazy evaluation takes
> place in the original environment, not in another one.
> 

I think I finally understand. My intuition just came from looking at
?delayedAssign.
But delayedAssign came to replace delay(), which "creates a promise to
evaluate the given expression".
When one thinks of delay(), what you said makes sense, you just delay
executing a certain expression in the parent frame.

I think, though, that with the current way it is described and called,
delayedAssign should by default only have the side effect of changing the
variable, i.e. use eval.env=new.env().

The manual states:
This function is invoked for its side effect, which is assigning a promise
to evaluate value to the variable x.

I think that is a nice clear side effect - changing a variable when it is
evaluated...like a delayed "<<-".
Otherwise it seems to me that delayedAssign could cause debugging
nightmares. Luckily, it currently doesn't seem to widely used to cause
them....

But you are right that it might be a bit strange that assign.env and
eval.env are different. Maybe that is why there are two different parameters
- to make the side effects clearer?
I tried to find anywhere uses of delayedAssign which make positive use of
side effects other than the assignment, and couldn't find any. Does anyone
know of such a use?

P.S. the end of ?delayedAssign contains this cryptic code:

e <- (function(x, y = 1, z) environment())(1+2, "y", {cat(" HO! "); pi+2})
(le <- as.list(e)) # evaluates the promises

Which I think is another way to create a promise, other than delayedAssign.
But it is really unclear why it sits there at the bottom of the document.
There should probably be more explanation of what this is....


--
View this message in context: http://r.789695.n4.nabble.com/delayedAssign-changing-values-tp4588108p4591137.html
Sent from the R devel mailing list archive at Nabble.com.



[Rd] delayedAssign changing values
ghostwheel