Skip to content
Prev 62169 / 63424 Next

NOTE: multiple local function definitions for ?fun? with different formal arguments

On 04/02/2024 10:55 a.m., Izmirlian, Grant (NIH/NCI) [E] via R-devel wrote:
I think something happened to your explanation...
It's a bad idea to use ifelse() when you really want if() ... else ... . 
  In this case it works, but it doesn't always.  So the workaround should be


toto <- function(mode)
{
     if(mode == 1)
         function(a,b) a*b
     else
         function(u, v, w) (u + v) / w
}
I agree it's a false positive, but the issue is that you have a function 
object in your function which can't be called unconditionally.  The 
workaround doesn't create such an object.

Recognizing that your function never tries to call fun requires global 
inspection of toto(), and most of the checks are based on local inspection.

Duncan Murdoch