Advanced-R-Solutions icon indicating copy to clipboard operation
Advanced-R-Solutions copied to clipboard

UseMethod local variable update for R 4.4.0

Open epi-sam opened this issue 2 months ago • 0 comments

Wondering if it's worth noting the update for R 4.4.0 here:

https://github.com/Tazinho/Advanced-R-Solutions/blob/cfdb2b5a09d6163f3888089e8ad9a2fa61b37989/13_S3.Rmd#L421

UseMethod creates a new function call with arguments matched as they came in to the generic. [Previously local variables defined before the call to UseMethod were retained; as of R 4.4.0 this is no longer the case.]

  • https://stat.ethz.ch/R-manual/R-devel/library/base/html/UseMethod.html

Reprex

sessionInfo()
#> R version 4.5.0 (2025-04-11 ucrt)
#> ...

g <- function(x){
   x <- 10
   y <- 10
   UseMethod('g')
}
g.default <- function(x) c(x = x, y = y)
x <- 1
y <- 1
g(x)
#>  x  y 
#>  1  1
g.default(x)
#> x y 
#> 1 1

rm(y)
g(x)
#> Error in g.default(x) : object 'y' not found

epi-sam avatar Nov 11 '25 22:11 epi-sam