Contrast the following two code snippets:
one)
> 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 fragment, g.numeric free variable (namely, “y”) is evaluated in the g local environment, while in the second fragment, g.numeric free variable (namely, “x”) is evaluated in the global environment. How is this?
generics oop r
Evan aad
source share