From deepayan.sarkar at gmail.com  Tue Sep 12 01:26:24 2006
From: deepayan.sarkar at gmail.com (Deepayan Sarkar)
Date: Mon, 11 Sep 2006 16:26:24 -0700
Subject: [Rd] unexpected behaviour when defining a function
Message-ID: <eb555e660609111626t3f03dd69q2e3ebfe845f6540c@mail.gmail.com>

Hi,

I know S manuals used to warn against using the same names for a
variable and a function, but I have never seen that cause problems in
R, so I usually don't pay much attention to it. Which is why the
following behaviour came as a surprise:

> bar <- function() 1
> foo <- function(bar = bar()) {
+     bar
+ }
> foo(9)
[1] 9
> foo()
Error in foo() : recursive default argument reference

Exactly what rule am I violating here?

The following gives a slightly different error, but I assume it has a
similar origin:

bar <- function() 1
foo <- function(bar) {
    if (missing(bar)) bar <- bar()
    bar
}
foo()

This version works fine though (so the rule probably involves function
arguments somehow):

foo <- function(baz) {
    if (missing(baz)) {
        baz <- function() 2
        baz <- baz()
    }
    baz
}
foo()

-Deepayan




[Rd] unexpected behaviour when defining a function
Deepayan Sarkar