Scope of methods invoked by UseMethod

Viewed 67

Contrast the following two code snippets:

1)

> y <- 1
> g <- function(x) { 
+   y <- 2
+   UseMethod("g")
+ }
> g.numeric <- function(x) y
> g(10)
[1] 2

2)

> x <- 1
> g <- function(x) {
+   x <- 2
+   UseMethod("g")
+ }
> g.numeric <- function(y) x
> g(10)
[1] 1

In the first snippet, g.numeric's free variable (namely, "y") is evaluated in g's local environment, whereas in the second snippet, g.numeric's free variable (namely "x") is evaluated in the global environment. How so?

1 Answers
Related