Re: [Rd] recursive default argument reference

From: Gabor Grothendieck <ggrothendieck_at_myway.com>
Date: Wed 10 Nov 2004 - 12:53:18 EST

Vadim Ogranovich <vograno <at> evafunds.com> writes:

:
: Hi,
:
: It seems that a formal function argument can not default to an "outer"
: variable of the same name:
:
: > x <- "foo"
: > ff <- function(x=x) x
: > ff()
: Error in ff() : recursive default argument reference
: >
:
: Is this intentional? Why?
:

Because then one arg could not safely refer to another. Here x has y as its default value:

   x <- 10
   y <- 7
   f <- function(x = y, y = 3) print(x)

   f() # prints 3

Note that you could do this:

   x <- "foo"
   ff <- function(x.=x) x.

   ff() # returns "foo"
   ff(x=44) # returns 44
   ff(55) # returns 55

For a real example look at merge.data.frame.



R-devel@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Wed Nov 10 12:59:55 2004

This archive was generated by hypermail 2.1.8 : Wed 10 Nov 2004 - 13:10:02 EST