[Rd] body(NULL) <- something; ditto formals() -- should not work

Hervé Pagès hpages at fredhutch.org
Tue Mar 29 21:19:12 CEST 2016


Hi,

On 03/07/2016 09:26 AM, Martin Maechler wrote:
> I'm proposing to signal an error (from R >= 3.3.0) in such
> examples -- which do "work" in R 3.2.x and earlier :
>
>> f <- NULL; body(f) <- quote(sin(a+1)); f
> function ()
> sin(a + 1)
> <environment: 0x48f9798>
>

This works because formals() (the getter) works on things that are
not a function.

>> g <- NULL; formals(g) <- alist(x = pi, y=); g
> function (x = pi, y)
> NULL
> <environment: 0x4e6dfe8>
>>

And this works because body() (the getter) also works on things that
are not a function.

>
> The proposal is that the underlying C code will signal an error
> when such replacement functions would create a function out of
> "something not a function".

Why modify the C code for this?

This is how formals() and body() are defined at the moment:

 > formals
function (fun = sys.function(sys.parent()))
{
     if (is.character(fun))
         fun <- get(fun, mode = "function", envir = parent.frame())
     .Internal(formals(fun))
}
<bytecode: 0x153cde0>
<environment: namespace:base>

 > body
function (fun = sys.function(sys.parent()))
{
     if (is.character(fun))
         fun <- get(fun, mode = "function", envir = parent.frame())
     .Internal(body(fun))
}
<bytecode: 0xb452e8>
<environment: namespace:base>

Not sure why they are not simply doing fun <- match.fun(fun) but if
they were, then they would signal an error when called on something
that is not a function.

H.

>
> Martin Maechler, ETH Zurich
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

-- 
Hervé Pagès

Program in Computational Biology
Division of Public Health Sciences
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N, M1-B514
P.O. Box 19024
Seattle, WA 98109-1024

E-mail: hpages at fredhutch.org
Phone:  (206) 667-5791
Fax:    (206) 667-1319



More information about the R-devel mailing list