Hard to go through all this post, but I think that this small example can help to demystify the NextMethod
dispatching.
I create an object with 2 classes attributes (inheritance) 'first' and 'second'.
x <- 1
attr(x,'class') <- c('first','second')
Then I create a generic
method Cat
to print my object
Cate <- function(x,...)UseMethod('Cate')
I define Cate
method for each class.
Cate.first <- function(x,...){
print(match.call())
print(paste('first:',x))
print('---------------------')
NextMethod() ## This will call Cate.second
}
Cate.second <- function(x,y){
print(match.call())
print(paste('second:',x,y))
}
Now you can can check Cate
call using this example:
Cate(x,1:3)
Cate.first(x = x, 1:3)
[1] "first: 1"
[1] "---------------------"
Cate.second(x = x, y = 1:3)
[1] "second: 1 1" "second: 1 2" "second: 1 3"
- For Cate.second the previous method is Cate.first
- Arguments x and y are passed down from the current method to the inherited method with their current values at the time NextMethod() is called.
- Argument y passed through the "..." formal argument arrive with the correct argument name
Cate.second(x = x, y = 1:3)