Re: [Rd] formals(x)<- drops attributes including class

2016-03-13 Thread Duncan Murdoch

On 13/03/2016 9:54 AM, Henrik Bengtsson wrote:

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:


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



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' 
  - 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)
}




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

/Henrik

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel



__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


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

2016-03-13 Thread Henrik Bengtsson
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' 
 - 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)
}




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

/Henrik

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel