| 18558 |
jmc |
1 |
\name{callNextMethod}
|
|
|
2 |
\alias{callNextMethod}
|
|
|
3 |
\title{Call an Inherited Method}
|
|
|
4 |
\description{
|
|
|
5 |
A call to \code{callNextMethod} can only appear inside a method
|
|
|
6 |
definition. It then results in a call to the first inherited method
|
|
|
7 |
after the current method, with the arguments to the current method
|
|
|
8 |
passed down to the next method. The value of that method call is the
|
|
|
9 |
value of \code{callNextMethod}.
|
|
|
10 |
}
|
|
|
11 |
\usage{
|
| 21345 |
jmc |
12 |
callNextMethod(...)
|
| 18558 |
jmc |
13 |
}
|
| 21345 |
jmc |
14 |
\arguments{
|
|
|
15 |
\item{\dots}{
|
| 23898 |
jmc |
16 |
Optionally, the arguments to the function in its next call
|
|
|
17 |
(but note that the dispatch is as in the detailed description below;
|
|
|
18 |
the arguments have no effect on selecting the next method.)
|
| 23838 |
jmc |
19 |
|
| 23898 |
jmc |
20 |
If no arguments are included in the call to \code{callNextMethod}, the
|
|
|
21 |
effect is to call the method with the current arguments.
|
|
|
22 |
See the detailed description for what this really means.
|
|
|
23 |
|
|
|
24 |
Calling with no arguments is often the natural way to use
|
|
|
25 |
\code{callNextMethod}; see the examples.
|
| 21345 |
jmc |
26 |
}
|
|
|
27 |
}
|
| 18558 |
jmc |
28 |
\details{
|
| 25118 |
hornik |
29 |
The \dQuote{next} method (i.e., the first inherited method) is defined to
|
| 23898 |
jmc |
30 |
be that method
|
| 18558 |
jmc |
31 |
which \emph{would} have been called if the current method did not
|
|
|
32 |
exist.
|
|
|
33 |
This is more-or-less literally what happens: The current method is
|
|
|
34 |
deleted from a copy of the methods for the current generic, and
|
|
|
35 |
\code{\link{selectMethod}} is called to find the next method (the
|
|
|
36 |
result is cached in a special object, so the search only typically
|
|
|
37 |
happens once per session per combination of argument classes).
|
|
|
38 |
|
|
|
39 |
It is also legal, and often useful, for the method called by
|
| 23898 |
jmc |
40 |
\code{callNextMethod} to itself have a call to
|
|
|
41 |
\code{callNextMethod}. This generally works as you would expect, but
|
|
|
42 |
for completeness be aware that it is possible to have ambiguous
|
|
|
43 |
inheritance in the S structure, in the sense that the same two
|
|
|
44 |
classes can appear as superclasses \emph{in the opposite order} in
|
|
|
45 |
two other class definitions. In this case the effect of a nested
|
|
|
46 |
instance of \code{callNextMethod} is not well defined. Such
|
|
|
47 |
inconsistent class hierarchies are both rare and nearly always the
|
|
|
48 |
result of bad design, but they are possible, and currently undetected.
|
| 18558 |
jmc |
49 |
|
|
|
50 |
The statement that the method is called with the current arguments is
|
|
|
51 |
more precisely as follows. Arguments that were missing in the current
|
|
|
52 |
call are still missing (remember that \code{"missing"} is a valid
|
|
|
53 |
class in a method signature). For a formal argument, say \code{x}, that
|
|
|
54 |
appears in the original call, there is a corresponding argument in the
|
| 25118 |
hornik |
55 |
next method call equivalent to \dQuote{\code{x = x}}. In effect, this
|
| 18558 |
jmc |
56 |
means that the next method sees the same actual arguments, but
|
|
|
57 |
arguments are evaluated only once.
|
|
|
58 |
}
|
|
|
59 |
\value{
|
|
|
60 |
The value returned by the selected method.
|
|
|
61 |
}
|
|
|
62 |
\references{
|
| 25118 |
hornik |
63 |
The R package \pkg{methods} implements, with a few exceptions, the
|
| 21345 |
jmc |
64 |
programming interface for classes
|
|
|
65 |
and methods in the book \emph{Programming with Data} (John
|
|
|
66 |
M. Chambers, Springer, 1998), in particular sections 1.6, 2.7, 2.8,
|
|
|
67 |
and chapters 7 and 8.
|
| 18558 |
jmc |
68 |
|
| 25118 |
hornik |
69 |
While the programming interface for the \pkg{methods} package follows
|
|
|
70 |
the reference, the R software is an original implementation, so
|
|
|
71 |
details in the reference that reflect the S4 implementation may appear
|
|
|
72 |
differently in R. Also, there are extensions to the programming
|
| 21345 |
jmc |
73 |
interface developed more recently than the reference. For a
|
|
|
74 |
discussion of details and ongoing development, see the web page
|
|
|
75 |
\url{http://developer.r-project.org/methodsPackage.html} and the
|
|
|
76 |
pointers from that page.
|
| 18558 |
jmc |
77 |
}
|
| 19029 |
hornik |
78 |
\seealso{\link{Methods} for the general behavior of method dispatch}
|
| 18558 |
jmc |
79 |
|
| 18976 |
jmc |
80 |
\examples{
|
| 18558 |
jmc |
81 |
|
|
|
82 |
## some class definitions with simple inheritance
|
| 19029 |
hornik |
83 |
setClass("B0" , representation(b0 = "numeric"))
|
| 18558 |
jmc |
84 |
|
| 24439 |
ripley |
85 |
setClass("B1", representation(b1 = "character"), contains = "B0")
|
| 18558 |
jmc |
86 |
|
| 24439 |
ripley |
87 |
setClass("B2", representation(b2 = "logical"), contains = "B1")
|
| 18558 |
jmc |
88 |
|
|
|
89 |
## and a rather silly function to illustrate callNextMethod
|
|
|
90 |
|
|
|
91 |
f <- function(x) class(x)
|
|
|
92 |
|
| 24439 |
ripley |
93 |
setMethod("f", "B0", function(x) c(x@b0^2, callNextMethod()))
|
|
|
94 |
setMethod("f", "B1", function(x) c(paste(x@b1,":"), callNextMethod()))
|
| 23898 |
jmc |
95 |
setMethod("f", "B2", function(x) c(x@b2, callNextMethod()))
|
| 18558 |
jmc |
96 |
|
| 24439 |
ripley |
97 |
b1 <- new("B1", b0 = 2, b1 = "Testing")
|
| 18558 |
jmc |
98 |
|
| 24439 |
ripley |
99 |
b2 <- new("B2", b2 = FALSE, b1 = "More testing", b0 = 10)
|
| 18558 |
jmc |
100 |
|
|
|
101 |
f(b2)
|
|
|
102 |
|
|
|
103 |
f(b1)
|
|
|
104 |
|
| 26000 |
ripley |
105 |
\dontshow{
|
| 23898 |
jmc |
106 |
|
| 24439 |
ripley |
107 |
stopifnot(identical(f(b2), c(b2@b2, paste(b2@b1,":"), b2@b0^2, "B2")))
|
| 23898 |
jmc |
108 |
|
| 24439 |
ripley |
109 |
## a version of the example with 1 more layer of nesting
|
| 23898 |
jmc |
110 |
|
| 24439 |
ripley |
111 |
## next methods calling next methods, with arguments; using group generics
|
| 23898 |
jmc |
112 |
setMethod("Ops", "B2",
|
|
|
113 |
function(e1, e2) callNextMethod())
|
|
|
114 |
setMethod("Ops", c("B0","numeric"),
|
|
|
115 |
function(e1, e2) callNextMethod(e1@b0, e2))
|
|
|
116 |
|
|
|
117 |
b2 + 1 # 11
|
|
|
118 |
|
|
|
119 |
b1 == 2 # TRUE
|
|
|
120 |
|
|
|
121 |
removeClass("B2"); removeClass("B1"); removeClass("B0")
|
|
|
122 |
|
|
|
123 |
removeGeneric("f")
|
|
|
124 |
|
|
|
125 |
removeMethods("Ops")
|
|
|
126 |
|
| 24563 |
ripley |
127 |
## tests of multiple callNextMethod
|
|
|
128 |
setClass("m1", representation(count = "numeric"), contains = "matrix",
|
|
|
129 |
prototype = prototype(count = 0))
|
|
|
130 |
mm1 <- new("m1", matrix(1:12, 3,4))
|
|
|
131 |
setMethod("[", "m1", function(x, i, j, ..., drop) callNextMethod())
|
|
|
132 |
|
|
|
133 |
setClass("m2", representation(sum = "numeric"), contains = "m1")
|
|
|
134 |
|
|
|
135 |
setMethod("Ops", c("m1", "m1"), function(e1, e2) {
|
|
|
136 |
as(e1, "matrix") <- callNextMethod()
|
|
|
137 |
e1@count <- max(e1@count, e2@count)+1
|
|
|
138 |
e1})
|
|
|
139 |
|
|
|
140 |
mm2 <- new("m2", matrix(1:12, 3, 4), sum = sum(1:12))
|
|
|
141 |
|
|
|
142 |
stopifnot(identical(mm2[,2], 4:6))
|
|
|
143 |
|
|
|
144 |
setClass("m3", representation(rowtags = "character"),contains = "m2")
|
|
|
145 |
|
|
|
146 |
setMethod("[", signature(x="m3", i = "character", j = "missing", drop = "missing"),
|
|
|
147 |
function(x, i,j, ..., drop) {
|
|
|
148 |
xx <- callNextMethod(x, match(i, x@rowtags),)
|
|
|
149 |
x@.Data <- xx
|
|
|
150 |
x@rowtags <- x@rowtags[match(i, x@rowtags)]
|
|
|
151 |
x})
|
|
|
152 |
|
|
|
153 |
tm = matrix(1:12, 4, 3)
|
|
|
154 |
|
|
|
155 |
mm3 = new("m3", tm, rowtags = letters[1:4])
|
|
|
156 |
|
|
|
157 |
mmm = mm3[c("b", "d")]
|
|
|
158 |
|
|
|
159 |
stopifnot(identical(mmm, new("m3", tm[c(2, 4),], rowtags = c("b", "d"))))
|
|
|
160 |
|
|
|
161 |
removeClass("m3")
|
|
|
162 |
removeClass("m2")
|
|
|
163 |
removeClass("m1")
|
|
|
164 |
|
|
|
165 |
removeMethods("[")
|
|
|
166 |
|
| 18558 |
jmc |
167 |
}
|
| 23898 |
jmc |
168 |
|
|
|
169 |
}
|
| 18558 |
jmc |
170 |
\keyword{programming}
|
|
|
171 |
\keyword{classes}
|
|
|
172 |
\keyword{methods}
|