| 42333 |
ripley |
1 |
% File src/library/methods/man/setGeneric.Rd
|
| 68948 |
ripley |
2 |
% Part of the R package, https://www.R-project.org
|
| 71228 |
maechler |
3 |
% Copyright 1995-2016 R Core Team
|
| 42333 |
ripley |
4 |
% Distributed under GPL 2 or later
|
|
|
5 |
|
| 15357 |
jmc |
6 |
\name{setGeneric}
|
| 56186 |
murdoch |
7 |
\alias{setGeneric}
|
| 71366 |
jmc |
8 |
\title{Create a Generic Version of a Function}
|
| 15357 |
jmc |
9 |
\description{
|
| 71366 |
jmc |
10 |
Create a generic version of the named function so that methods may
|
|
|
11 |
be defined for it. A call to \code{\link{setMethod}} will call
|
|
|
12 |
\code{setGeneric} automatically if applied to a non-generic
|
|
|
13 |
function.
|
|
|
14 |
|
|
|
15 |
An explicit call to \code{setGeneric} is usually not required, but
|
|
|
16 |
doesn't hurt and makes explicit that methods are being defined for a
|
|
|
17 |
non-generic function.
|
|
|
18 |
|
|
|
19 |
Standard calls will be of the form:
|
| 88914 |
smeyer |
20 |
\preformatted{
|
|
|
21 |
setGeneric(name)
|
|
|
22 |
}
|
| 71366 |
jmc |
23 |
where \code{name} specifies an existing function, possibly in another
|
|
|
24 |
package. An alternative when creating a new generic function in this package is:
|
| 88914 |
smeyer |
25 |
\preformatted{
|
|
|
26 |
setGeneric(name, def)
|
|
|
27 |
}
|
| 71388 |
ripley |
28 |
where the function definition \code{def} specifies the formal
|
| 71366 |
jmc |
29 |
arguments and becomes the default method.
|
|
|
30 |
|
| 15357 |
jmc |
31 |
}
|
|
|
32 |
\usage{
|
| 44243 |
ripley |
33 |
setGeneric(name, def= , group=list(), valueClass=character(),
|
|
|
34 |
where= , package= , signature= , useAsDefault= ,
|
| 46546 |
jmc |
35 |
genericFunction= , simpleInheritanceOnly = )
|
| 15357 |
jmc |
36 |
}
|
|
|
37 |
\arguments{
|
| 45824 |
jmc |
38 |
\item{name}{ The character string name of the generic function.
|
| 15571 |
ripley |
39 |
}
|
| 71366 |
jmc |
40 |
\item{def}{An optional function object, defining the non-generic
|
|
|
41 |
version, to become the default method. This is equivalent in
|
|
|
42 |
effect to assigning \code{def} as the function and then using
|
|
|
43 |
the one-argument call to \code{setGeneric}.
|
| 88914 |
smeyer |
44 |
}
|
| 26530 |
maechler |
45 |
|
| 71366 |
jmc |
46 |
\emph{The following arguments are specialized, optionally used
|
|
|
47 |
when creating a new generic function with non-standard
|
|
|
48 |
features. They should not be used when the non-generic is in
|
|
|
49 |
another package.}
|
| 58136 |
jmc |
50 |
|
| 71366 |
jmc |
51 |
\item{group}{ The name of the group
|
| 69538 |
ripley |
52 |
generic function to which this function belongs. See
|
| 71366 |
jmc |
53 |
\link{Methods_Details} for details of group generic functions in method
|
| 71388 |
ripley |
54 |
selection and \link{S4groupGeneric} for existing groups.
|
| 15571 |
ripley |
55 |
}
|
| 71366 |
jmc |
56 |
\item{valueClass}{ A character vector specifying one or more class
|
| 50786 |
maechler |
57 |
names. The value returned by the generic function must
|
| 42679 |
ripley |
58 |
have (or extend) this class, or one of the classes; otherwise,
|
| 45824 |
jmc |
59 |
an error is generated.
|
| 42679 |
ripley |
60 |
}
|
| 21345 |
jmc |
61 |
\item{signature}{
|
| 71366 |
jmc |
62 |
The vector of names from among the formal arguments to
|
|
|
63 |
the function, that will be allowed in the signature of methods for this
|
|
|
64 |
function, in calls to \code{\link{setMethod}}. By default and
|
|
|
65 |
usually, this will be all formal arguments except \code{\dots}.
|
|
|
66 |
|
|
|
67 |
A non-standard signature for the generic function may be
|
|
|
68 |
used to exclude arguments that take advantage of lazy evaluation;
|
|
|
69 |
in particular, if the argument may \emph{not} be evaluated then it
|
|
|
70 |
cannot be part of the signature.
|
|
|
71 |
|
|
|
72 |
While \code{\dots} cannot be used as part of a general signature,
|
|
|
73 |
it is possible to have this as the \emph{only} element of the
|
|
|
74 |
signature.
|
|
|
75 |
Methods will then be selected if their signature matches
|
| 69538 |
ripley |
76 |
all the \code{\dots} arguments. See the documentation for topic
|
| 71366 |
jmc |
77 |
\link{dotsMethods} for details. It is not
|
|
|
78 |
possible to mix \code{\dots} and other arguments in the signature.
|
| 46314 |
jmc |
79 |
|
| 71366 |
jmc |
80 |
It's usually a mistake to omit arguments from the signature in the
|
|
|
81 |
belief that this improves efficiency. For method selection, the
|
|
|
82 |
arguments that are used in the signatures for the \emph{methods}
|
|
|
83 |
are what counts, and then only seriously on the first call to the
|
|
|
84 |
function with that combination of classes.
|
| 15571 |
ripley |
85 |
}
|
| 46546 |
jmc |
86 |
\item{simpleInheritanceOnly}{
|
| 50786 |
maechler |
87 |
Supply this argument as \code{TRUE} to require that methods selected
|
|
|
88 |
be inherited through simple inheritance only; that is, from
|
|
|
89 |
superclasses specified in the \code{contains=} argument to
|
|
|
90 |
\code{\link{setClass}}, or by simple inheritance to a class union or
|
|
|
91 |
other virtual class. Generic functions should require simple
|
|
|
92 |
inheritance if they need to be assured that they get the complete
|
|
|
93 |
original object, not one that has been transformed. Examples of
|
|
|
94 |
functions requiring simple inheritance are \code{\link{initialize}},
|
|
|
95 |
because by definition it must return an object from the same class
|
|
|
96 |
as its argument, and \code{\link{show}}, because it claims to give a
|
|
|
97 |
full description of the object provided as its argument.
|
| 71366 |
jmc |
98 |
|
| 46546 |
jmc |
99 |
}
|
| 71366 |
jmc |
100 |
\item{useAsDefault}{
|
|
|
101 |
Override the usual default method mechanism. Only relevant when
|
|
|
102 |
defining a nonstandard generic function.
|
|
|
103 |
See the section \sQuote{Specialized Local Generics}.
|
| 88914 |
smeyer |
104 |
}
|
| 18558 |
jmc |
105 |
|
| 71366 |
jmc |
106 |
\emph{The remaining arguments are obsolete for normal applications.}
|
| 88914 |
smeyer |
107 |
|
| 71366 |
jmc |
108 |
\item{package}{ The name of the package with which this function is
|
|
|
109 |
associated. Should be determined automatically from the
|
|
|
110 |
non-generic version.
|
|
|
111 |
|
|
|
112 |
}
|
|
|
113 |
\item{where}{ Where to store the resulting objects as side effects.
|
|
|
114 |
The default, to store in the package's namespace, is the only
|
|
|
115 |
safe choice.
|
|
|
116 |
}
|
|
|
117 |
\item{genericFunction}{Obsolete.}
|
| 15357 |
jmc |
118 |
}
|
| 45824 |
jmc |
119 |
\section{Basic Use}{
|
| 15357 |
jmc |
120 |
The \code{setGeneric} function is called to initialize a generic
|
| 50786 |
maechler |
121 |
function as preparation for defining some methods for that function.
|
| 15357 |
jmc |
122 |
|
| 71366 |
jmc |
123 |
The simplest and most common situation is that \code{name} specifies
|
|
|
124 |
an existing function, usually in another package. You now want to
|
|
|
125 |
define methods for this function. In this case you should
|
| 50786 |
maechler |
126 |
supply only \code{name}, for example:
|
| 88914 |
smeyer |
127 |
\preformatted{
|
|
|
128 |
setGeneric("colSums")
|
|
|
129 |
}
|
| 71366 |
jmc |
130 |
There must be an existing function of this name (in this case in
|
|
|
131 |
package \code{"base"}). The non-generic function can be in the same
|
|
|
132 |
package as the call, typically the case when you are creating a new
|
|
|
133 |
function plus methods for it. When the function is in
|
|
|
134 |
another package, it must be available by name, for
|
|
|
135 |
example through an \code{importFrom()} directive in this package's
|
|
|
136 |
\code{NAMESPACE} file. Not required for functions in \code{"base"},
|
|
|
137 |
which are implicitly imported.
|
|
|
138 |
|
|
|
139 |
A generic version of
|
|
|
140 |
the function will be created in the current package. The existing function
|
| 50786 |
maechler |
141 |
becomes the default method, and the package slot of the new generic
|
|
|
142 |
function is set to the location of the original function
|
| 71366 |
jmc |
143 |
(\code{"base"} in the example).
|
| 45824 |
jmc |
144 |
|
| 71366 |
jmc |
145 |
Two special types of non-generic should be noted.
|
|
|
146 |
Functions that dispatch S3 methods by calling
|
|
|
147 |
\code{\link{UseMethod}} are ordinary functions, not objects from the
|
|
|
148 |
\code{"genericFunction"} class. They are made generic like any
|
|
|
149 |
other function, but some special considerations apply to ensure that
|
|
|
150 |
S4 and S3 method dispatch is consistent (see \link{Methods_for_S3}).
|
| 45824 |
jmc |
151 |
|
| 71366 |
jmc |
152 |
Primitive functions are handled in C code and don't exist as normal
|
|
|
153 |
functions.
|
|
|
154 |
A call to \code{setGeneric} is allowed in the simple form, but no
|
|
|
155 |
actual generic function object is created. Method dispatch will
|
|
|
156 |
take place in the C code. See the section on Primitive Functions for
|
|
|
157 |
more details.
|
| 45824 |
jmc |
158 |
|
| 71366 |
jmc |
159 |
It's an important feature that the
|
|
|
160 |
identical generic function definition is created in every package that
|
|
|
161 |
uses the same \code{setGeneric()} call.
|
|
|
162 |
When any of these packages is loaded into an \R session, this
|
|
|
163 |
function will be added to a table of generic functions, and will
|
|
|
164 |
contain a methods table of all the available methods for the
|
|
|
165 |
function.
|
| 69636 |
lawrence |
166 |
|
| 71366 |
jmc |
167 |
Calling \code{setGeneric()} is not strictly
|
|
|
168 |
necessary before calling \code{setMethod()}. If
|
|
|
169 |
the function specified in the call to \code{setMethod} is not generic,
|
|
|
170 |
\code{setMethod} will execute the call to \code{setGeneric} itself.
|
|
|
171 |
In the case that the non-generic is in another package, does not
|
|
|
172 |
dispatch S3 methods and is not a primitive, a message is printed noting the
|
|
|
173 |
creation of the generic function the first time \code{setMethod} is called.
|
| 45824 |
jmc |
174 |
|
| 50786 |
maechler |
175 |
The second common use of \code{setGeneric()} is to create a new
|
| 71366 |
jmc |
176 |
generic function, unrelated to any existing function. See the
|
|
|
177 |
\code{asRObject()} example below.
|
|
|
178 |
This case can be handled just like the previous examples, with only
|
|
|
179 |
the difference that the non-generic function exists in the
|
|
|
180 |
current package.
|
|
|
181 |
Again, the non-generic version becomes the default method.
|
|
|
182 |
For clarity it's best for the assignment to immediately precede the
|
|
|
183 |
call to \code{setGeneric()} in the source code.
|
| 18976 |
jmc |
184 |
|
| 71366 |
jmc |
185 |
Exactly the same result can be obtained by supplying the default as
|
|
|
186 |
the \code{def} argument instead of assigning it.
|
|
|
187 |
In some applications, there will be no completely general default
|
|
|
188 |
method. While there is a special mechanism for this (see the
|
|
|
189 |
\sQuote{Specialized Local Generics} section), the recommendation is to provide a
|
|
|
190 |
default method that signals an error, but with a message that
|
|
|
191 |
explains as clearly as you can why a non-default method is needed.
|
| 18976 |
jmc |
192 |
|
| 45824 |
jmc |
193 |
}
|
| 71366 |
jmc |
194 |
\section{Specialized Local Generics}{
|
| 58136 |
jmc |
195 |
The great majority of calls to \code{setGeneric()} should either
|
|
|
196 |
have one argument to ensure that an existing function can have
|
|
|
197 |
methods, or arguments \code{name} and \code{def} to create a new
|
| 71366 |
jmc |
198 |
generic function and optionally a default method.
|
| 58136 |
jmc |
199 |
|
| 71366 |
jmc |
200 |
It is possible to create generic functions with nonstandard
|
|
|
201 |
signatures, or functions that do additional computations besides
|
|
|
202 |
method dispatch or that belong to a group of generic functions.
|
|
|
203 |
|
|
|
204 |
None of these mechanisms should be used with a non-generic function
|
|
|
205 |
from a \emph{different} package, because the result is to create a
|
|
|
206 |
generic function that may not be consistent from one package to another.
|
|
|
207 |
When any such options are used,
|
|
|
208 |
the new generic function will be assigned with a
|
| 50786 |
maechler |
209 |
package slot set to the \emph{current} package, not the one in which
|
| 71366 |
jmc |
210 |
the non-generic version of the function is found.
|
| 18976 |
jmc |
211 |
|
| 71366 |
jmc |
212 |
There is a mechanism to define a specialized generic version of a
|
|
|
213 |
non-generic function, the \code{\link{implicitGeneric}}
|
|
|
214 |
construction.
|
|
|
215 |
This defines the generic version, but then reverts the function to
|
|
|
216 |
it non-generic form, saving the implicit generic in a table to be
|
|
|
217 |
activated when methods are defined.
|
|
|
218 |
However, the mechanism can only legitimately be used either for a non-generic
|
|
|
219 |
in the same package or by the \code{"methods"} package itself.
|
|
|
220 |
And in the first case, there is no compelling reason not to simply
|
|
|
221 |
make the function generic, with the non-generic as the default
|
|
|
222 |
method.
|
|
|
223 |
See \code{\link{implicitGeneric}} for details.
|
|
|
224 |
|
| 50786 |
maechler |
225 |
The body of a generic function usually does nothing except for
|
|
|
226 |
dispatching methods by a call to \code{standardGeneric}. Under some
|
|
|
227 |
circumstances you might just want to do some additional computation in
|
|
|
228 |
the generic function itself. As long as your function eventually
|
| 71366 |
jmc |
229 |
calls \code{standardGeneric} that is permissible.
|
|
|
230 |
See the example \code{"authorNames"} below.
|
| 20676 |
jmc |
231 |
|
| 71366 |
jmc |
232 |
In this case, the \code{def} argument will define the nonstandard
|
|
|
233 |
generic, not the default method.
|
|
|
234 |
An existing non-generic of the same name and calling sequence should
|
|
|
235 |
be pre-assigned. It will become the default method, as usual.
|
|
|
236 |
(An alternative is the \code{useAsDefault} argument.)
|
|
|
237 |
|
| 42656 |
ripley |
238 |
By default, the generic function can return any object. If
|
|
|
239 |
\code{valueClass} is supplied, it should be a vector of class names;
|
|
|
240 |
the value returned by a method is then required to satisfy
|
|
|
241 |
\code{is(object, Class)} for one of the specified classes. An empty
|
|
|
242 |
(i.e., zero length) vector of classes means anything is allowed. Note
|
|
|
243 |
that more complicated requirements on the result can be specified
|
|
|
244 |
explicitly, by defining a non-standard generic function.
|
| 18976 |
jmc |
245 |
|
| 71366 |
jmc |
246 |
If the \code{def} argument calls \code{standardGeneric()} (with or
|
|
|
247 |
without additional computations) and there is no existing
|
|
|
248 |
non-generic version of the function, the generic is created without
|
|
|
249 |
a default method. This is not usually a good idea: better to have a
|
|
|
250 |
default method that signals an error with a message explaining why
|
|
|
251 |
the default case is not defined.
|
| 45824 |
jmc |
252 |
|
| 71366 |
jmc |
253 |
A new generic function can be created belonging to an existing group
|
|
|
254 |
by including the \code{group} argument. The argument list of the
|
|
|
255 |
new generic must agree with that of the group. See
|
|
|
256 |
\code{\link{setGroupGeneric}} for defining a new group generic.
|
|
|
257 |
For the role of group generics in
|
|
|
258 |
dispatching methods, see \link{GroupGenericFunctions} and section
|
|
|
259 |
10.5 of the second reference.
|
| 45824 |
jmc |
260 |
|
|
|
261 |
|
| 71366 |
jmc |
262 |
}
|
| 45824 |
jmc |
263 |
|
| 19473 |
jmc |
264 |
\section{Generic Functions and Primitive Functions}{
|
| 41161 |
maechler |
265 |
A number of the basic \R functions are specially implemented as
|
| 50786 |
maechler |
266 |
primitive functions, to be evaluated directly in the underlying C code
|
|
|
267 |
rather than by evaluating an \R language definition. Most have
|
| 42299 |
ripley |
268 |
implicit generics (see \code{\link{implicitGeneric}}), and become
|
|
|
269 |
generic as soon as methods (including group methods) are defined on
|
|
|
270 |
them. Others cannot be made generic.
|
| 19473 |
jmc |
271 |
|
| 71366 |
jmc |
272 |
|
|
|
273 |
Calling \code{setGeneric()} for
|
|
|
274 |
the primitive functions in the base package differs in that it does not, in fact,
|
|
|
275 |
generate an explicit generic function.
|
|
|
276 |
Methods for primitives are selected and dispatched from
|
|
|
277 |
the internal C code, to satisfy concerns for efficiency.
|
|
|
278 |
The same is true for a few
|
|
|
279 |
non-primitive functions that dispatch internally. These include
|
|
|
280 |
\code{unlist} and \code{as.vector}.
|
|
|
281 |
|
|
|
282 |
Note, that the implementation restrict methods for
|
| 50786 |
maechler |
283 |
primitive functions to signatures in which at least one of the classes
|
|
|
284 |
in the signature is a formal S4 class.
|
| 71366 |
jmc |
285 |
Otherwise the internal C code will not look for methods.
|
|
|
286 |
This is a desirable restriction in principle, since optional
|
|
|
287 |
packages should not be allowed to change the behavior of basic R
|
|
|
288 |
computations on existing data types.
|
| 45824 |
jmc |
289 |
|
|
|
290 |
To see the generic version of a primitive function, use
|
|
|
291 |
\code{\link{getGeneric}(name)}. The function
|
|
|
292 |
\code{\link{isGeneric}} will tell you whether methods are defined
|
|
|
293 |
for the function in the current session.
|
|
|
294 |
|
| 48062 |
jmc |
295 |
Note that S4 methods can only be set on those primitives which are
|
| 90185 |
jagan |
296 |
\sQuote{\link{internal generic}}.
|
| 19473 |
jmc |
297 |
}
|
| 71366 |
jmc |
298 |
|
| 15357 |
jmc |
299 |
\value{
|
| 19029 |
hornik |
300 |
The \code{setGeneric} function exists for its side effect: saving the
|
|
|
301 |
generic function to allow methods to be specified later. It returns
|
|
|
302 |
\code{name}.
|
| 15357 |
jmc |
303 |
}
|
|
|
304 |
\references{
|
| 88585 |
hornik |
305 |
\bibshow{R:Chambers:2016}
|
|
|
306 |
(Chapters 9 and 10.)
|
| 71366 |
jmc |
307 |
|
| 88585 |
hornik |
308 |
\bibshow{R:Chambers:2008}
|
|
|
309 |
(Section 10.5 for some details.)
|
| 15357 |
jmc |
310 |
}
|
| 20676 |
jmc |
311 |
\examples{
|
|
|
312 |
|
| 71366 |
jmc |
313 |
## Specify that this package will define methods for plot()
|
|
|
314 |
setGeneric("plot")
|
|
|
315 |
|
| 45824 |
jmc |
316 |
## create a new generic function, with a default method
|
| 58136 |
jmc |
317 |
setGeneric("props", function(object) attributes(object))
|
| 45824 |
jmc |
318 |
|
| 20676 |
jmc |
319 |
### A non-standard generic function. It insists that the methods
|
|
|
320 |
### return a non-empty character vector (a stronger requirement than
|
|
|
321 |
### valueClass = "character" in the call to setGeneric)
|
|
|
322 |
|
| 26530 |
maechler |
323 |
setGeneric("authorNames",
|
| 20676 |
jmc |
324 |
function(text) {
|
|
|
325 |
value <- standardGeneric("authorNames")
|
|
|
326 |
if(!(is(value, "character") && any(nchar(value)>0)))
|
|
|
327 |
stop("authorNames methods must return non-empty strings")
|
|
|
328 |
value
|
|
|
329 |
})
|
|
|
330 |
|
| 71366 |
jmc |
331 |
## the asRObject generic function, from package XR
|
|
|
332 |
## Its default method just returns object
|
|
|
333 |
## See the reference, Chapter 12 for methods
|
|
|
334 |
|
|
|
335 |
setGeneric("asRObject", function(object, evaluator) {
|
|
|
336 |
object
|
|
|
337 |
})
|
|
|
338 |
|
|
|
339 |
|
| 26000 |
ripley |
340 |
\dontshow{
|
| 20676 |
jmc |
341 |
setMethod("authorNames", "character", function(text)text)
|
|
|
342 |
|
| 49727 |
maechler |
343 |
tryIt <- function(expr) tryCatch(expr, error = function(e) e)
|
| 39651 |
maechler |
344 |
stopifnot(identical(authorNames(c("me", "you")), c("me", "you")),
|
| 49727 |
maechler |
345 |
is(tryIt(authorNames(character())), "error"), # empty value
|
|
|
346 |
is(tryIt(authorNames(NULL)), "error")) # no default method
|
| 20676 |
jmc |
347 |
}
|
| 26000 |
ripley |
348 |
\dontshow{
|
| 20676 |
jmc |
349 |
removeGeneric("authorNames")
|
| 45824 |
jmc |
350 |
removeGeneric("props")
|
| 71366 |
jmc |
351 |
removeGeneric("asRObject")
|
| 19474 |
jmc |
352 |
}
|
|
|
353 |
}
|
| 15571 |
ripley |
354 |
\seealso{
|
| 71366 |
jmc |
355 |
\code{\link{Methods_Details}} and the links there for a general discussion,
|
| 46314 |
jmc |
356 |
\code{\link{dotsMethods}} for methods that dispatch on
|
| 69538 |
ripley |
357 |
\code{\dots}, and \code{\link{setMethod}} for method definitions.
|
| 15571 |
ripley |
358 |
}
|
|
|
359 |
\keyword{ programming }
|
|
|
360 |
\keyword{ methods }
|
| 71366 |
jmc |
361 |
|
|
|
362 |
|
|
|
363 |
% The description above is the effect when the package that owns the
|
|
|
364 |
% non-generic function has not created an implicit generic version.
|
|
|
365 |
% Otherwise, it is this implicit generic function that is used. See the
|
|
|
366 |
% section on Implicit Generic Functions below. Either way, the
|
|
|
367 |
% essential result is that the \emph{same} version of the generic
|
|
|
368 |
% function will be created each time.
|
|
|
369 |
|
|
|
370 |
% The \code{useAsDefault} argument controls the default method for the
|
|
|
371 |
% new generic. If not told otherwise, \code{setGeneric} will try to
|
|
|
372 |
% find a non-generic version of the function to use as a default. So,
|
|
|
373 |
% if you do have a suitable default method, it is often simpler to first
|
|
|
374 |
% set this up as a non-generic function, and then use the one-argument
|
|
|
375 |
% call to \code{setGeneric} at the beginning of this section. See the
|
|
|
376 |
% first example in the Examples section below.
|
|
|
377 |
|
|
|
378 |
% If you \emph{don't} want the existing function to be taken as default,
|
|
|
379 |
% supply the argument \code{useAsDefault}. That argument can be the
|
|
|
380 |
% function you want to be the default method, or \code{FALSE} to force
|
|
|
381 |
% no default (i.e., to cause an error if there is no direct or inherited
|
|
|
382 |
% method selected for a call to the function).
|