[R] Evaluation of defaults in functions

Duncan Murdoch murdoch at stats.uwo.ca
Fri Sep 29 13:07:28 CEST 2006


On 9/28/2006 10:34 PM, hadley wickham wrote:
>> And, to answer the specific question: Yes, R has lazy evaluation,
>> everywhere. Arguments are always evaluated if and when they are
>> needed.
>>
> 
> But doesn't R has a rather limited force of lazy evaluation? - you
> have no control over it, apart from that arguments are evaluated
> lazily.  This rather limited compared to other languages (no lazy
> lists etc)

You do have more control than that.  You can't put a promise in a list, 
but you can put one in an environment, e.g.

 > x <- new.env()
 > y <- 1
 > delayedAssign("z", y, assign=x)
 > y <- 2
 > x$z
[1] 2

A few versions ago there was a delay() function that might have let you 
do this as

x$z <- delay(y)

but this doesn't really make sense:  since assignment is a function 
call, why wouldn't it force the evaluation of the promise?  The 
contortions necessary to work around this contradiction led to some 
strange errors, so delay was deprecated and is now defunct.  (I don't 
really know what the result of the assignment above would have been.)

Duncan Murdoch



More information about the R-help mailing list