r-lib / rlang

Low-level API for programming with R
https://rlang.r-lib.org
Other
491 stars 131 forks source link

Should `inject()` strip srcrefs? #1665

Open hadley opened 8 months ago

hadley commented 8 months ago

To avoid this problem:

library(rlang)
power1 <- function(exp) {
  inject(function(x) x ^ !!exp)
}
funs <- lapply(1:2, power1)
funs
#> [[1]]
#> function(x) x ^ !!exp
#> <environment: 0x107033e08>
#> 
#> [[2]]
#> function(x) x ^ !!exp
#> <environment: 0x107032ba8>

lapply(funs, \(f) structure(f, srcref= NULL))
#> [[1]]
#> function (x) 
#> x^1L
#> <environment: 0x107033e08>
#> 
#> [[2]]
#> function (x) 
#> x^2L
#> <environment: 0x107032ba8>

Created on 2023-11-02 with reprex v2.0.2