[R] Scope?

Douglas Bates bates at stat.wisc.edu
Sun Feb 10 00:47:11 CET 2002

Göran Broström <gb at stat.umu.se> writes:

> I find the following behaviour slightly disturbing:
> > x = 6
> > ex = function(x = x) x
> > ex()
> Error in ex() : recursive default argument reference
> Can't  R  distinguish between formal and actual arguments? (Just kidding!)
> Seriously, what bad things could happen if I was allowed to get the 
> answer  6  here?

You would lose lazy evaluation.  The default value of an argument is
evaluated at the time that the argument is first accessed during
function evaluation, not at the time of definition of the function.
That allows you to define default values of arguments in terms of
other values calculated within the function.
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list