Skip to content
Prev 24987 / 398502 Next

function showMethods and inheritance

Laurent Gautier wrote:
There isn't quite enough information in your question to be sure, but
the following is a likely explanation.

If you look at ?showMethods, under the argument "inherited" you'll see
the comment that "methods that have been found by inheritance, so far in
the session" will be shown.  The cost of searching for inherited methods
is `amortized', in that inherited methods are stored only after they
have been needed.  So until you actually call the relevant function with
an argument from class B, you won't see the method in showMethods.

Here's an example, roughly along your lines. (By the way, one point of
possible confusion:  _classes_ don't have methods, _functions_ have
methods corresponding to the classes of the arguments.  But let's call
the function myMethod to keep to the example.)

R> setClass("A", representation(a="character"))
[1] "A"
R> setClass("B", representation(b="numeric"), contains = "A")
[1] "B"
R> setGeneric("myMethod", function(x)standardGeneric("myMethod"))
[1] "myMethod"
R> setMethod("myMethod", "A", function(x)x at a)
[1] "myMethod"
R> b1 <- new("B", a="Testing", b = 1:10)
R> showMethods("myMethod", inherited = TRUE)

Function "myMethod":
x = "A"
R> myMethod(b1)
[1] "Testing"
R> showMethods("myMethod", inherited = TRUE)

Function "myMethod":
x = "A"
x = "B"
    (inherited from x = "A")

Another way to see what method would be used with class "B" (if that was
the original purpose) is to call selectMethod, which does the same
method selection as calling the function:

R> selectMethod("myMethod", "B")
Method Definition (Class "MethodDefinition"):

function(x)x at a

Signatures:
        x  
target  "B"
defined "A"

In the printed version of the method, "target" is the class signature
for which the method would be used, and "defined" is the signature with
which setMethod was called.