| 24500 |
ripley |
1 |
\name{UseMethod}
|
|
|
2 |
\title{Class Methods}
|
|
|
3 |
\alias{UseMethod}
|
|
|
4 |
\alias{NextMethod}
|
|
|
5 |
\alias{S3Methods}
|
|
|
6 |
\alias{.isMethodsDispatchOn}
|
|
|
7 |
\description{
|
|
|
8 |
\R possesses a simple generic function mechanism which can be used for
|
|
|
9 |
an object-oriented style of programming. Method despatch takes place
|
|
|
10 |
based on the class of the first argument to the generic function or on
|
|
|
11 |
the object supplied as an argument to \code{UseMethod} or \code{NextMethod}.
|
|
|
12 |
}
|
|
|
13 |
\usage{
|
|
|
14 |
UseMethod(generic, object)
|
|
|
15 |
NextMethod(generic = NULL, object = NULL, \dots)
|
|
|
16 |
}
|
|
|
17 |
\arguments{
|
|
|
18 |
\item{generic}{a character string naming a function.}
|
|
|
19 |
\item{object}{an object whose class will determine the method to be
|
|
|
20 |
dispatched. Defaults to the first argument of the enclosing function.}
|
|
|
21 |
\item{\dots}{further arguments to be passed to the method.}
|
|
|
22 |
}
|
|
|
23 |
\details{
|
| 25118 |
hornik |
24 |
An \R \dQuote{object} is a data object which has a \code{class} attribute.
|
| 24500 |
ripley |
25 |
A class attribute is a character vector giving the names of
|
| 25118 |
hornik |
26 |
the classes which the object \dQuote{inherits} from.
|
| 24500 |
ripley |
27 |
If the object does not have a class attribute, it has an implicit
|
|
|
28 |
class, \code{"matrix"}, \code{"array"} or the result of
|
|
|
29 |
\code{\link{mode}(x)}.
|
|
|
30 |
|
|
|
31 |
When a generic
|
|
|
32 |
function \code{fun} is applied to an object with class attribute
|
|
|
33 |
\code{c("first", "second")}, the system searches for a function called
|
|
|
34 |
\code{fun.first} and, if it finds it, applied it to the object. If no
|
|
|
35 |
such function is found a function called \code{fun.second} is tried.
|
|
|
36 |
If no class name produces a suitable function, the function
|
|
|
37 |
\code{fun.default} is used.
|
|
|
38 |
|
| 27447 |
ripley |
39 |
Function \code{\link[utils]{methods}} can be used to find out about the
|
| 24500 |
ripley |
40 |
methods for a particular generic function or class.
|
|
|
41 |
|
|
|
42 |
Now for some obscure details that need to appear somewhere. These
|
|
|
43 |
comments will be slightly different than those in Appendix A of the
|
| 25118 |
hornik |
44 |
White S Book. \code{UseMethod} creates a \dQuote{new} function call with
|
| 24500 |
ripley |
45 |
arguments matched as they came in to the generic. Any local variables
|
| 24520 |
ripley |
46 |
defined before the call to \code{UseMethod} are retained (unlike S). Any
|
| 24500 |
ripley |
47 |
statements after the call to \code{UseMethod} will not be evaluated as
|
|
|
48 |
\code{UseMethod} does not return. \code{UseMethod} can be called with
|
|
|
49 |
more than two arguments: a warning will be given and additional
|
| 24520 |
ripley |
50 |
arguments ignored. (They are not completely ignored in S.) If it is
|
|
|
51 |
called with just one argument, the class of the first argument of the
|
|
|
52 |
enclosing function is used as \code{object}: unlike S this is the
|
|
|
53 |
actual argument passed and not the current value of the object of that
|
|
|
54 |
name.
|
| 24500 |
ripley |
55 |
|
|
|
56 |
\code{NextMethod} invokes the next method (determined by the
|
|
|
57 |
class). It does this by creating a special call frame for that
|
|
|
58 |
method. The arguments will be the same in number, order and name as
|
|
|
59 |
those to the current method but their values will be promises to
|
|
|
60 |
evaluate their name in the current method and environment. Any
|
|
|
61 |
arguments matched to \code{\dots} are handled specially. They are
|
|
|
62 |
passed on as the promise that was supplied as an argument to the
|
|
|
63 |
current environment. (S does this differently!) If they have been
|
|
|
64 |
evaluated in the current (or a previous environment) they remain
|
|
|
65 |
evaluated.
|
|
|
66 |
|
|
|
67 |
\code{NextMethod} should not be called except in methods called by
|
|
|
68 |
\code{UseMethod}. In particular it will not work inside anonymous
|
|
|
69 |
calling functions (eg \code{get("print.ts")(AirPassengers)}).
|
|
|
70 |
|
| 25669 |
luke |
71 |
Name spaces can register methods for generic functions. To support
|
|
|
72 |
this, \code{UseMethod} and \code{NextMethod} search for methods in
|
|
|
73 |
two places: first in the environment in which the generic function
|
|
|
74 |
is called, and then in the registration data base for the
|
|
|
75 |
environment in which the generic is defined (typically a name space).
|
|
|
76 |
So methods for a generic function need to either be available in the
|
|
|
77 |
environment of the call to the generic, or they must be registered.
|
|
|
78 |
It does not matter whether they are visible in the environment in
|
|
|
79 |
which the generic is defined.
|
|
|
80 |
|
| 24500 |
ripley |
81 |
}
|
|
|
82 |
\note{
|
|
|
83 |
This scheme is called \emph{S3} (S version 3). For new projects,
|
|
|
84 |
it is recommended to use the more flexible and robust \emph{S4} scheme
|
| 25118 |
hornik |
85 |
provided in the \pkg{methods} package.
|
| 24520 |
ripley |
86 |
|
| 26478 |
jmc |
87 |
The function \code{.isMethodsDispatchOn()} returns \code{TRUE} if
|
|
|
88 |
the S4 method dispatch has been turned on in the evaluator. It is
|
|
|
89 |
meant for \R internal use only.
|
| 24500 |
ripley |
90 |
}
|
|
|
91 |
\seealso{
|
| 27447 |
ripley |
92 |
\code{\link[utils]{methods}}, \code{\link[base]{class}}, \code{\link[utils]{getS3method}}
|
| 24500 |
ripley |
93 |
}
|
|
|
94 |
\references{
|
|
|
95 |
Chambers, J. M. (1992)
|
|
|
96 |
\emph{Classes and methods: object-oriented programming in S.}
|
|
|
97 |
Appendix A of \emph{Statistical Models in S}
|
|
|
98 |
eds J. M. Chambers and T. J. Hastie, Wadsworth \& Brooks/Cole.
|
|
|
99 |
}
|
|
|
100 |
\keyword{methods}
|