[Rd] as.list fails on functions with S3 classes
Antoine Fabri
@nto|ne@|@br| @end|ng |rom gm@||@com
Wed Apr 28 22:45:00 CEST 2021
Dear R devel,
as.list() can be used on functions, but not if they have a S3 class that
doesn't include "function".
See below :
```r
add1 <- function(x) x+1
as.list(add1)
#> $x
#>
#>
#> [[2]]
#> x + 1
class(add1) <- c("function", "foo")
as.list(add1)
#> $x
#>
#>
#> [[2]]
#> x + 1
class(add1) <- "foo"
as.list(add1)
#> Error in as.vector(x, "list"): cannot coerce type 'closure' to vector of
type 'list'
as.list.function(add1)
#> $x
#>
#>
#> [[2]]
#> x + 1
```
In failing case the argument is dispatched to as.list.default instead of
as.list.function.
(1) Shouldn't it be dispatched to as.list.function ?
(2) Shouldn't all generics when applied on an object of type closure fall
back to the `fun.function` method before falling back to the `fun.default`
method ?
Best regards,
Antoine
[[alternative HTML version deleted]]
More information about the R-devel
mailing list