| 42333 |
ripley |
1 |
% File src/library/base/man/UseMethod.Rd
|
| 68948 |
ripley |
2 |
% Part of the R package, https://www.R-project.org
|
| 67599 |
ripley |
3 |
% Copyright 1995-2014 R Core Team
|
| 42333 |
ripley |
4 |
% Distributed under GPL 2 or later
|
|
|
5 |
|
| 24500 |
ripley |
6 |
\name{UseMethod}
|
|
|
7 |
\title{Class Methods}
|
| 56186 |
murdoch |
8 |
\alias{UseMethod}
|
| 24500 |
ripley |
9 |
\alias{NextMethod}
|
|
|
10 |
\alias{S3Methods}
|
| 38429 |
ripley |
11 |
\alias{.Method}
|
|
|
12 |
\alias{.Generic}
|
|
|
13 |
\alias{.Class}
|
| 24500 |
ripley |
14 |
\description{
|
|
|
15 |
\R possesses a simple generic function mechanism which can be used for
|
| 36334 |
ripley |
16 |
an object-oriented style of programming. Method dispatch takes place
|
| 38429 |
ripley |
17 |
based on the class(es) of the first argument to the generic function or of
|
| 24500 |
ripley |
18 |
the object supplied as an argument to \code{UseMethod} or \code{NextMethod}.
|
|
|
19 |
}
|
|
|
20 |
\usage{
|
|
|
21 |
UseMethod(generic, object)
|
| 38429 |
ripley |
22 |
|
| 24500 |
ripley |
23 |
NextMethod(generic = NULL, object = NULL, \dots)
|
|
|
24 |
}
|
|
|
25 |
\arguments{
|
| 38148 |
ripley |
26 |
\item{generic}{a character string naming a function (and not a
|
|
|
27 |
built-in operator). Required for \code{UseMethod}.}
|
| 37776 |
ripley |
28 |
\item{object}{for \code{UseMethod}: an object whose class will
|
|
|
29 |
determine the method to be dispatched. Defaults to the first
|
|
|
30 |
argument of the enclosing function.}
|
|
|
31 |
\item{\dots}{further arguments to be passed to the next method.}
|
| 24500 |
ripley |
32 |
}
|
|
|
33 |
\details{
|
| 42961 |
ripley |
34 |
An \R object is a data object which has a \code{class}
|
| 41079 |
ripley |
35 |
attribute (and this can be tested by \code{\link{is.object}}).
|
| 24500 |
ripley |
36 |
A class attribute is a character vector giving the names of
|
| 42961 |
ripley |
37 |
the classes from which the object \emph{inherits}.
|
| 24500 |
ripley |
38 |
If the object does not have a class attribute, it has an implicit
|
| 32148 |
ripley |
39 |
class. Matrices and arrays have class \code{"matrix"}
|
|
|
40 |
or\code{"array"} followed by the class of the underlying vector.
|
| 36334 |
ripley |
41 |
Most vectors have class the result of \code{\link{mode}(x)}, except
|
| 32148 |
ripley |
42 |
that integer vectors have class \code{c("integer", "numeric")} and
|
|
|
43 |
real vectors have class \code{c("double", "numeric")}.
|
| 24500 |
ripley |
44 |
|
| 38148 |
ripley |
45 |
When a function calling \code{UseMethod("fun")} is applied to an
|
|
|
46 |
object with class attribute \code{c("first", "second")}, the system
|
|
|
47 |
searches for a function called \code{fun.first} and, if it finds it,
|
|
|
48 |
applies it to the object. If no such function is found a function
|
|
|
49 |
called \code{fun.second} is tried. If no class name produces a
|
|
|
50 |
suitable function, the function \code{fun.default} is used, if it
|
|
|
51 |
exists, or an error results.
|
| 24500 |
ripley |
52 |
|
| 30461 |
ripley |
53 |
Function \code{\link{methods}} can be used to find out about the
|
| 24500 |
ripley |
54 |
methods for a particular generic function or class.
|
|
|
55 |
|
| 65147 |
ripley |
56 |
\code{UseMethod} is a primitive function but uses standard argument
|
|
|
57 |
matching. It is not the only means of dispatch of methods, for there
|
|
|
58 |
are \link{internal generic} and \link{group generic} functions.
|
|
|
59 |
\code{UseMethod} currently dispatches on the implicit class even for
|
|
|
60 |
arguments that are not objects, but the other means of dispatch do
|
|
|
61 |
not.
|
| 36054 |
ripley |
62 |
|
| 24500 |
ripley |
63 |
\code{NextMethod} invokes the next method (determined by the
|
| 39493 |
ripley |
64 |
class vector, either of the object supplied to the generic, or of
|
| 37776 |
ripley |
65 |
the first argument to the function containing \code{NextMethod} if a
|
|
|
66 |
method was invoked directly). Normally \code{NextMethod} is used with
|
|
|
67 |
only one argument, \code{generic}, but if further arguments are
|
| 38429 |
ripley |
68 |
supplied these modify the call to the next method.
|
| 24500 |
ripley |
69 |
|
|
|
70 |
\code{NextMethod} should not be called except in methods called by
|
| 39493 |
ripley |
71 |
\code{UseMethod} or from internal generics (see
|
|
|
72 |
\link{InternalGenerics}). In particular it will not work inside
|
| 66444 |
hornik |
73 |
anonymous calling functions (e.g., \code{get("print.ts")(AirPassengers)}).
|
| 24500 |
ripley |
74 |
|
| 56382 |
murdoch |
75 |
Namespaces can register methods for generic functions. To support
|
| 25669 |
luke |
76 |
this, \code{UseMethod} and \code{NextMethod} search for methods in
|
| 74235 |
hornik |
77 |
two places: in the environment in which the generic function
|
|
|
78 |
is called, and in the registration data base for the
|
| 56382 |
murdoch |
79 |
environment in which the generic is defined (typically a namespace).
|
| 38148 |
ripley |
80 |
So methods for a generic function need to be available in the
|
| 25669 |
luke |
81 |
environment of the call to the generic, or they must be registered.
|
| 38148 |
ripley |
82 |
(It does not matter whether they are visible in the environment in
|
| 74235 |
hornik |
83 |
which the generic is defined.) As from \R 3.5.0, the registration
|
|
|
84 |
data base is searched after the top level environment (see
|
|
|
85 |
\code{\link{topenv}}) of the calling environment (but before the
|
|
|
86 |
parents of the top level environment).
|
| 24500 |
ripley |
87 |
}
|
| 38429 |
ripley |
88 |
\section{Technical Details}{
|
|
|
89 |
Now for some obscure details that need to appear somewhere. These
|
|
|
90 |
comments will be slightly different than those in Chambers(1992).
|
|
|
91 |
(See also the draft \sQuote{R Language Definition}.)
|
| 42961 |
ripley |
92 |
\code{UseMethod} creates a new function call with
|
| 38429 |
ripley |
93 |
arguments matched as they came in to the generic. Any local variables
|
|
|
94 |
defined before the call to \code{UseMethod} are retained (unlike S). Any
|
|
|
95 |
statements after the call to \code{UseMethod} will not be evaluated as
|
|
|
96 |
\code{UseMethod} does not return. \code{UseMethod} can be called with
|
|
|
97 |
more than two arguments: a warning will be given and additional
|
|
|
98 |
arguments ignored. (They are not completely ignored in S.) If it is
|
|
|
99 |
called with just one argument, the class of the first argument of the
|
|
|
100 |
enclosing function is used as \code{object}: unlike S this is the first
|
|
|
101 |
actual argument passed and not the current value of the object of that
|
|
|
102 |
name.
|
|
|
103 |
|
|
|
104 |
\code{NextMethod} works by creating a special call frame for the next
|
|
|
105 |
method. If no new arguments are supplied, the arguments will be the
|
|
|
106 |
same in number, order and name as those to the current method but
|
|
|
107 |
their values will be promises to evaluate their name in the current
|
|
|
108 |
method and environment. Any named arguments matched to \code{\dots}
|
|
|
109 |
are handled specially: they either replace existing arguments of the
|
|
|
110 |
same name or are appended to the argument list. They are passed on as
|
|
|
111 |
the promise that was supplied as an argument to the current
|
| 55555 |
ripley |
112 |
environment. (S does this differently!) If they have been evaluated
|
| 38429 |
ripley |
113 |
in the current (or a previous environment) they remain evaluated.
|
|
|
114 |
(This is a complex area, and subject to change: see the draft
|
|
|
115 |
\sQuote{R Language Definition}.)
|
|
|
116 |
|
| 39508 |
ripley |
117 |
The search for methods for \code{NextMethod} is slightly different
|
|
|
118 |
from that for \code{UseMethod}. Finding no \code{fun.default} is not
|
|
|
119 |
necessarily an error, as the search continues to the generic
|
| 55555 |
ripley |
120 |
itself. This is to pick up an \link{internal generic} like \code{[}
|
| 39508 |
ripley |
121 |
which has no separate default method, and succeeds only if the generic
|
|
|
122 |
is a \link{primitive} function or a wrapper for a
|
|
|
123 |
\code{\link{.Internal}} function of the same name. (When a primitive
|
|
|
124 |
is called as the default method, argument matching may not work as
|
|
|
125 |
described above due to the different semantics of primitives.)
|
|
|
126 |
|
| 38429 |
ripley |
127 |
You will see objects such as \code{.Generic}, \code{.Method}, and
|
|
|
128 |
\code{.Class} used in methods. These are set in the environment
|
|
|
129 |
within which the method is evaluated by the dispatch mechanism, which
|
|
|
130 |
is as follows:
|
|
|
131 |
\enumerate{
|
|
|
132 |
\item Find the context for the calling function (the generic): this
|
|
|
133 |
gives us the unevaluated arguments for the original call.
|
|
|
134 |
\item Evaluate the object (usually an argument) to be used for
|
|
|
135 |
dispatch, and find a method (possibly the default method) or throw
|
|
|
136 |
an error.
|
|
|
137 |
\item Create an environment for evaluating the method and insert
|
|
|
138 |
special variables (see below) into that environment. Also copy any
|
|
|
139 |
variables in the environment of the generic that are not formal (or
|
|
|
140 |
actual) arguments.
|
|
|
141 |
\item Fix up the argument list to be the arguments of the call
|
|
|
142 |
matched to the formals of the method.
|
|
|
143 |
}
|
|
|
144 |
\code{.Generic} is a length-one character vector naming the generic function.
|
|
|
145 |
|
|
|
146 |
\code{.Method} is a character vector (normally of length one) naming
|
|
|
147 |
the method function. (For functions in the group generic
|
| 49567 |
ripley |
148 |
\code{\link[=S3groupGeneric]{Ops}} it is of length two.)
|
| 38429 |
ripley |
149 |
|
|
|
150 |
\code{.Class} is a character vector of classes used to find the next
|
|
|
151 |
method. \code{NextMethod} adds an attribute \code{"previous"} to
|
|
|
152 |
\code{.Class} giving the \code{.Class} last used for dispatch, and
|
|
|
153 |
shifts \code{.Class} along to that used for dispatch.
|
|
|
154 |
|
|
|
155 |
\code{.GenericCallEnv} and \code{.GenericDefEnv} are the environments
|
|
|
156 |
of the call to be generic and defining the generic respectively. (The
|
|
|
157 |
latter is used to find methods registered for the generic.)
|
|
|
158 |
|
|
|
159 |
Note that \code{.Class} is set when the generic is called, and is
|
|
|
160 |
unchanged if the class of the dispatching argument is changed in a
|
|
|
161 |
method. It is possible to change the method that \code{NextMethod}
|
|
|
162 |
would dispatch by manipulating \code{.Class}, but \sQuote{this is not
|
|
|
163 |
recommended unless you understand the inheritance mechanism
|
| 65208 |
hornik |
164 |
thoroughly} (Chambers & Hastie, 1992, p.\sspace{}469).
|
| 38429 |
ripley |
165 |
}
|
| 24500 |
ripley |
166 |
\note{
|
|
|
167 |
This scheme is called \emph{S3} (S version 3). For new projects,
|
|
|
168 |
it is recommended to use the more flexible and robust \emph{S4} scheme
|
| 38429 |
ripley |
169 |
provided in the \pkg{methods} package.
|
| 24500 |
ripley |
170 |
}
|
|
|
171 |
\seealso{
|
| 37776 |
ripley |
172 |
The draft \sQuote{R Language Definition}.
|
| 38429 |
ripley |
173 |
|
| 49567 |
ripley |
174 |
\code{\link{methods}}, \code{\link{class}},
|
| 41079 |
ripley |
175 |
\code{\link{getS3method}}, \code{\link{is.object}}.
|
| 24500 |
ripley |
176 |
}
|
|
|
177 |
\references{
|
|
|
178 |
Chambers, J. M. (1992)
|
|
|
179 |
\emph{Classes and methods: object-oriented programming in S.}
|
|
|
180 |
Appendix A of \emph{Statistical Models in S}
|
| 47262 |
ripley |
181 |
eds J. M. Chambers and T. J. Hastie, Wadsworth & Brooks/Cole.
|
| 24500 |
ripley |
182 |
}
|
|
|
183 |
\keyword{methods}
|