[Rd] removeSource() vs. function literals
Ivan Krylov
kry|ov@r00t @end|ng |rom gm@||@com
Thu Mar 30 16:32:39 CEST 2023
Dear R-devel,
In a package of mine, I use removeSource on expression objects in order
to make expressions that are semantically the same serialize to the
same byte sequences:
https://github.com/cran/depcache/blob/854d68a/R/fixup.R#L8-L34
Today I learned that expressions containing function definitions also
contain the source references for the functions, not as an attribute,
but as a separate argument to the `function` call:
str(quote(function() NULL)[[4]])
# 'srcref' int [1:8] 1 11 1 25 11 25 1 1
# - attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile'
# <environment:0x55aba55a8a50>
This means that removeSource() on an expression that would define a
function when evaluated doesn't actually remove the source reference
from the object.
Do you think it would be appropriate to teach removeSource() to remove
such source references? What could be a good way to implement that?
if (is.call(fn) && identical(fn[[1]], 'function')) fn[[4]] <- NULL
sounds too arbitrary. if (inherits(fn, 'srcref')) return(NULL) sounds
too broad.
--
Best regards,
Ivan
More information about the R-devel
mailing list