[Rd] Exchange of information between functions aside from argument/return-value interface

Luke Tierney luke at stat.uiowa.edu
Wed Apr 4 20:45:25 CEST 2007

On Wed, 4 Apr 2007, Peter Ruckdeschel wrote:

> Thank you, Luke, for your promptous response.
> [snipped away my original message; to be found on
> https://stat.ethz.ch/pipermail/r-devel/2007-April/045216.html]
>> Sounds like lexical scoping should work for this also -- use <<- to
>> assign to the variable in the containing scope.
>> Best,
>> luke
> yes, in my application, this really solves the problem. Thank you.
> But I am not quite sure for the general situation:
> Recall that we have the sequence of calls
> myF() calls notmyG() which --with possibly more functions involved in between--
> directly or indirectly calls myH().
> Now, if I do not know exactly which variables are defined
> in the intermediate notmyG() or in some of the functions called "in between",
> I would have thought that using
> VariableInMyF <<- something
> within myH() bears the danger that I am overwriting
> a variable of name VariableInMyF in notmyG() and "in-between's"
> instead of in myF().
> Or did I get something wrong?

I think you did. You seem to be thinking in terms of dynamic scope,
which looks for variables in the caller and then the caller's caller.

Lexical scope addresses exactly this problem.  In lexical scope
variable references are resolved on the basis of how function
definitions are nested in the written code (hence the term lexical).
If you define myH inside myF then free variables in myH are looked up
first in myF and then in the context where myF is defined. How myH is
called does not matter.  Nested functions in Pascal work the same way
if that helps.

[In terms of the internal implementation, the enclosing environment of
the environment of myH is the frame of the call to myF that created



THis is exactly the pro
> Thanks for your help
> Peter

Luke Tierney
Chair, Statistics and Actuarial Science
Ralph E. Wareham Professor of Mathematical Sciences
University of Iowa                  Phone:             319-335-3386
Department of Statistics and        Fax:               319-335-3017
    Actuarial Science
241 Schaeffer Hall                  email:      luke at stat.uiowa.edu
Iowa City, IA 52242                 WWW:  http://www.stat.uiowa.edu

More information about the R-devel mailing list