Function f1 is called with parameter B, which has a value of 20.
No, I do not think so. It is called with parameter a , which has the same meaning as B in the global environment. B not directly involved at this point.
Then you assign 100 to other B that you call f1.B in your message. (Note that following the previous statement, B is created here, not rewritten.)
Then, using the operator <<- , it crosses the scope, moving from f2 (where there is no B ) to f1 , where it finds it " f1.B " and assigns 1000.
Similarly, when using <<- on a it passes. It does not find a in f2 or f1 , but does it in a global environment and assigns it there.
Then you print to the old original B , which never changed.
Via:
<<- and ->> (...) invokes a search in the parent environments for the existing definition of the variable being assigned. If such a variable is found (and its binding is not blocked), then its value is overridden, otherwise the assignment occurs in the global environment.
So, for B "such a variable is found", and for a "assignment occurs in a global environment."
Conclusion: <<- confusing, and often best avoided.
source share