[Rd] utils::isS3stdGeneric chokes on primitives and identity

Antoine Fabri @nto|ne@|@br| @end|ng |rom gm@||@com
Thu Aug 20 00:40:28 CEST 2020


Dear R-devel,

utils::isS3stdGeneric tries to subset the body of the function it's fed,
primitives don't like that because they don't have a body, identity doesn't
like it either because it's body is a symbol.

According to the doc, any function is a legal input.

See below:

identity
#> function (x)
#> x
#> <bytecode: 0x0000000013d6da28>
#> <environment: namespace:base>

max
#> function (..., na.rm = FALSE)  .Primitive("max")

isS3stdGeneric(identity)
#> Error in bdexpr[[1L]]: objet de type 'symbol' non indiçable

isS3stdGeneric(max)
#> Error in while (as.character(bdexpr[[1L]]) == "{") bdexpr <-
bdexpr[[2L]]: l'argument est de longueur nulle

Here is a simple fix :

isS3stdGeneric <- function(f) {
  {
    bdexpr <- body(f)
    if(is.null(bdexpr) || !is.call(bdexpr)) return(FALSE)
    while (as.character(bdexpr[[1L]]) == "{") bdexpr <- bdexpr[[2L]]
    ret <- is.call(bdexpr) && identical(bdexpr[[1L]], as.name("UseMethod"))
    if (ret)
      names(ret) <- bdexpr[[2L]]
    ret
  }
}

isS3stdGeneric(identity)
#> [1] FALSE
isS3stdGeneric(max)
#> [1] FALSE

Best,

Antoine

	[[alternative HTML version deleted]]



More information about the R-devel mailing list