From henrik.bengtsson at gmail.com  Sun Mar 13 14:54:09 2016
From: henrik.bengtsson at gmail.com (Henrik Bengtsson)
Date: Sun, 13 Mar 2016 06:54:09 -0700
Subject: [Rd] formals(x)<- drops attributes including class
Message-ID: <CAFDcVCR3pqH-9qOiexCgpNmjcu2BzFGTG8LJLaYmqC0FC2yO2A@mail.gmail.com>

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:
> fcn <- structure(function() {}, foo="foo", class=c("foo"))
> str(fcn)
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"

> formals(fcn) <- list(a=1)
> str(fcn)
function (a = 1)
> attributes(fcn)
NULL


TROUBLESHOOTING:
>From the definition of formals()<-, it's quite clear why this happens:

> `formals<-`
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



[Rd] formals(x)<- drops attributes including class
Henrik Bengtsson