Skip to content
Prev 378305 / 398502 Next

Function in default parameter value closing over variables defined later in the enclosing function

On 23/01/2019 5:27 a.m., Jan T Kim wrote:
You are misinterpreting that section.  It's not the call stack that is 
searched, it's the chain of environments that starts with the evaluation 
frame of the current function.  Those are very different.  For example,


g <- function() {
   print(secret)
}

f <- function() {
   secret <- "secret"
   g()
}

would fail, because even though secret is defined in the caller of g() 
and is therefore in the call stack, that's irrelevant:  it's not in g's 
evaluation frame (which has no variables) or its parent (which is the 
global environment if those definitions were evaluated there).

Duncan Murdoch