The difference between functions f1 and f2 is the following. In f1 standard lexical scoping to find x will be used. In f2 evaluation of x is done in parent environment in the calling stack. This could be called a one level dynamic scoping, because in parent environment x is found using lexical scoping. If they are both called from global environment their behavior is identical.
However if they are called from within a function the results will differ:
We can see that f1 selects a variable from its lexical scope (global environment) and f2 from calling function fun.
An interesting thing is that function’s f1 lexical scope can be changed by assigning new environment to function f1 within function fun. This forces fun environment into lexical search path of f1 and is another way to simulate one level dynamic scoping.
The second method is useful when a function we want to call is defined externally (for example within a package) and we are unable to change it. The drawback is that called function may stop working because within its internals it might call functions or seek variables that are undefined within changed environment search path – so one has to be cautious with it.
In my next post I plan show an application of this idea on some practical example.