method()
takes a generic and signature and retrieves the corresponding
method. This is rarely needed because most of the time you'll rely on the
the generic, via R7_dispatch()
, to find and call the method for you.
However, this introspection is useful if you want to see the implementation
of a specific method.
Arguments
- generic
A generic function, either created by
new_generic()
,new_external_generic()
, or an existing S3 generic.- class, object
Perform introspection either with a
class
(processed withas_class()
) or a concreteobject
. Ifgeneric
uses multiple dispatch then bothobject
andclass
must be a list of classes/objects.
See also
method_explain()
to explain why a specific method was picked.
Examples
# Create a generic and register some methods
bizarro <- new_generic("bizarro", "x")
method(bizarro, class_numeric) <- function(x) rev(x)
method(bizarro, new_S3_class("factor")) <- function(x) {
levels(x) <- rev(levels(x))
x
}
# Printing the generic shows the registered method
bizarro
#> <R7_generic> bizarro(x, ...) with 3 methods:
#> 1: method(bizarro, "integer")
#> 2: method(bizarro, "double")
#> 3: method(bizarro, new_S3_class("factor"))
# And you can use method() to inspect specific implementations
method(bizarro, class = class_integer)
#> <R7_method> method(bizarro, "integer")
#> function (x)
#> rev(x)
#> <environment: 0x55aa137aa5e8>
method(bizarro, object = 1)
#> <R7_method> method(bizarro, "double")
#> function (x)
#> rev(x)
#> <environment: 0x55aa137aa5e8>
method(bizarro, new_S3_class("factor"))
#> <R7_method> method(bizarro, new_S3_class("factor"))
#> function (x)
#> {
#> levels(x) <- rev(levels(x))
#> x
#> }
#> <environment: 0x55aa137aa5e8>