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