r-lib / rlang

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

print function object does not recognise changed formals #1662

Open pawelru opened 11 months ago

pawelru commented 11 months ago

Printing the function object does not recognise changes to formals done via fn_fmls()<-. On the other hand str() as well as behaviour of the function is correct.

foo <- function(a = 1, b = 2) {c(a, b)}

# print
foo
#> function(a = 1, b = 2) {c(a, b)}
str(foo)
#> function (a = 1, b = 2)  
#>  - attr(*, "srcref")= 'srcref' int [1:8] 1 8 1 39 8 39 1 1
#>   ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x7f7fcfa73350>
rlang::fn_fmls(foo)
#> $a
#> [1] 1
#> 
#> $b
#> [1] 2
# eval
foo()
#> [1] 1 2

# assign
rlang::fn_fmls(foo) <- list(a = 10, b = 20)

# print
foo   # <- here!!!!
#> function(a = 1, b = 2) {c(a, b)}
str(foo)
#> function (a = 10, b = 20)  
#>  - attr(*, "srcref")= 'srcref' int [1:8] 1 8 1 39 8 39 1 1
#>   ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x7f7fcfa73350>
rlang::fn_fmls(foo)
#> $a
#> [1] 10
#> 
#> $b
#> [1] 20
# eval
foo()
#> [1] 10 20

Created on 2023-10-31 with reprex v2.0.2

This could be connected with https://github.com/r-lib/rlang/issues/1648