Skip to content

formals(x)<- drops attributes including class

2 messages · Henrik Bengtsson, Duncan Murdoch

#
Just checking in to see whether it is intended or not that assigning
new formals to a function/closure causes any attributes to be dropped:

EXAMPLE:
function ()
 - attr(*, "srcref")=Class 'srcref'  atomic [1:8] 1 18 1 30 18 30 1 1
  .. ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x000
000000c00d128>
 - attr(*, "foo")= chr "foo"
 - attr(*, "class")= chr "foo"
function (a = 1)
NULL


TROUBLESHOOTING:
function (fun, envir = environment(fun), value)
{
    bd <- body(fun)
    as.function(c(value, if (is.null(bd) || is.list(bd)) list(bd) else bd),
        envir)
}
<bytecode: 0x000000000b97a848>
<environment: namespace:base>


I'm fine with this, but I just wanted to make sure it's not overlooked.

/Henrik
#
On 13/03/2016 9:54 AM, Henrik Bengtsson wrote:
For srcref, yes.  Changing the formals or body of a function would 
usually make them invalid.

I think it makes a reasonable default behaviour for other attributes as 
well.

Duncan Murdoch