| 42333 |
ripley |
1 |
% File src/library/methods/man/implicitGeneric.Rd
|
| 68948 |
ripley |
2 |
% Part of the R package, https://www.R-project.org
|
| 59039 |
ripley |
3 |
% Copyright 1995-2007 R Core Team
|
| 42333 |
ripley |
4 |
% Distributed under GPL 2 or later
|
|
|
5 |
|
| 42246 |
jmc |
6 |
\name{implicitGeneric}
|
| 56186 |
murdoch |
7 |
\alias{implicitGeneric}
|
| 42246 |
jmc |
8 |
\alias{setGenericImplicit}
|
|
|
9 |
\alias{prohibitGeneric}
|
|
|
10 |
\alias{registerImplicitGenerics}
|
| 43625 |
ripley |
11 |
\alias{implicit generic}
|
| 42251 |
ripley |
12 |
\title{Manage Implicit Versions of Generic Functions}
|
| 42246 |
jmc |
13 |
\description{
|
| 71366 |
jmc |
14 |
The implicit generic mechanism stores generic versions of
|
|
|
15 |
functions
|
|
|
16 |
in a table in a package. The package does not want the current
|
|
|
17 |
version of the function to be a generic, however, and retains the
|
|
|
18 |
non-generic version.
|
|
|
19 |
|
|
|
20 |
When a call to \code{\link{setMethod}} or
|
|
|
21 |
\code{\link{setGeneric}} creates a generic version for one of these
|
|
|
22 |
functions, the object in the table is used.
|
|
|
23 |
This mechanism is only needed if special arguments were used to
|
|
|
24 |
create the generic; e.g., the \code{signature} or the \code{valueClass}
|
|
|
25 |
options.
|
|
|
26 |
|
|
|
27 |
Function \code{implicitGeneric()} returns the implicit
|
| 42251 |
ripley |
28 |
generic version, \code{setGenericImplicit()} turns a generic implicit,
|
|
|
29 |
\code{prohibitGeneric()} prevents your function from being made
|
|
|
30 |
generic, and \code{registerImplicitGenerics()} saves a set of implicit
|
|
|
31 |
generic definitions in the cached table of the current session.
|
| 42246 |
jmc |
32 |
}
|
|
|
33 |
\usage{
|
|
|
34 |
implicitGeneric(name, where, generic)
|
|
|
35 |
setGenericImplicit(name, where, restore = TRUE)
|
|
|
36 |
prohibitGeneric(name, where)
|
|
|
37 |
registerImplicitGenerics(what, where)
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
\arguments{
|
| 42251 |
ripley |
41 |
\item{name}{ Character string name of the function.}
|
|
|
42 |
\item{where}{ Package or environment in which to register the implicit
|
|
|
43 |
generics. When using the functions from the top level of your own
|
| 71366 |
jmc |
44 |
package source, this argument should be omitted.}
|
|
|
45 |
\item{generic}{ Obsolete, and likely to be deprecated.}
|
| 42251 |
ripley |
46 |
\item{restore}{Should the non-generic version of the function be
|
| 71366 |
jmc |
47 |
restored?.}
|
|
|
48 |
\item{what}{Optional table of
|
|
|
49 |
the implicit generics to register, but nearly always omitted, when
|
|
|
50 |
it defaults to a standard metadata name.}
|
| 42246 |
jmc |
51 |
}
|
|
|
52 |
\details{
|
|
|
53 |
|
| 71366 |
jmc |
54 |
Multiple packages may define methods for the same function, to apply
|
|
|
55 |
to classes defined in that package. Arithmetic and other operators,
|
|
|
56 |
\code{plot()} and many other basic computations are typical
|
|
|
57 |
examples. It's essential that all such packages write methods for
|
|
|
58 |
the \emph{same} definition of the generic function. So long as that
|
|
|
59 |
generic uses the default choice for signature and other parameters,
|
|
|
60 |
nothing needs to be done.
|
| 42246 |
jmc |
61 |
|
| 71366 |
jmc |
62 |
If the generic has special properties, these need to be ensured for
|
|
|
63 |
all packages creating methods for it. The simplest solution is just
|
|
|
64 |
to make the function generic in the package that originally owned
|
|
|
65 |
it. If for some reason the owner(s) of that package are unwilling
|
|
|
66 |
to do this, the alternative is to define the correct generic,
|
|
|
67 |
save it in a special table and restore the non-generic version by
|
|
|
68 |
calling \code{setGenericImplicit}.
|
|
|
69 |
|
| 42246 |
jmc |
70 |
|
| 71366 |
jmc |
71 |
Note that the package containing the function can define methods for the implicit generic as
|
| 42251 |
ripley |
72 |
well; when the implicit generic is made a real generic, those methods
|
|
|
73 |
will be included.
|
| 42246 |
jmc |
74 |
|
| 71366 |
jmc |
75 |
The usual reason for having a
|
| 42251 |
ripley |
76 |
non-default implicit generic is to provide a non-default signature,
|
|
|
77 |
and the usual reason for \emph{that} is to allow lazy evaluation of
|
| 71366 |
jmc |
78 |
some arguments. All arguments in the signature of a
|
| 42251 |
ripley |
79 |
generic function must be evaluated at the time the function needs to
|
| 71366 |
jmc |
80 |
select a method.
|
|
|
81 |
In the base function \code{with()} in the example below, evaluation of the argument
|
|
|
82 |
\code{expr} must be delayed; therefore, it is excluded from the signature.
|
| 42246 |
jmc |
83 |
|
| 42251 |
ripley |
84 |
If you want to completely prohibit anyone from turning your function
|
|
|
85 |
into a generic, call \code{prohibitGeneric()}.
|
| 71366 |
jmc |
86 |
|
|
|
87 |
Function \code{implicitGeneric()} returns the implicit generic
|
|
|
88 |
version of the named function. If there is no table of these or if
|
|
|
89 |
this function is not in the table, the result of a simple call
|
|
|
90 |
\code{setGeneric(name)} is returned.
|
| 42246 |
jmc |
91 |
}
|
| 71366 |
jmc |
92 |
|
|
|
93 |
\section{Implicit Generics for Base Functions}{
|
|
|
94 |
Implicit generic versions exist for some functions in the packages
|
|
|
95 |
supplied in the distribution of \R itself. These are stored in the
|
|
|
96 |
\sQuote{methods} package itself and will always be available.
|
|
|
97 |
|
|
|
98 |
As emphasized repeatedly in the documentation,
|
|
|
99 |
\code{\link{setGeneric}()} calls for a function in another package
|
|
|
100 |
should never have non-default settings for arguments such as
|
|
|
101 |
\code{signature}.
|
|
|
102 |
The reasoning applies specially to functions in supplied packages,
|
|
|
103 |
since methods for these are likely to exist in multiple packages.
|
|
|
104 |
A call to \code{implicitGeneric()} will show the generic version.
|
|
|
105 |
}
|
|
|
106 |
|
| 42246 |
jmc |
107 |
\value{
|
| 42251 |
ripley |
108 |
Function \code{implicitGeneric()} returns the implicit generic
|
|
|
109 |
definition (and caches that definition the first time if it has to
|
|
|
110 |
construct it).
|
| 42389 |
maechler |
111 |
|
| 42251 |
ripley |
112 |
The other functions exist for their side effect and return nothing
|
|
|
113 |
useful.
|
| 42246 |
jmc |
114 |
}
|
| 71366 |
jmc |
115 |
|
| 42246 |
jmc |
116 |
\seealso{\code{\link{setGeneric}}}
|
|
|
117 |
\examples{
|
| 42389 |
maechler |
118 |
|
| 71366 |
jmc |
119 |
### How we would make the function with() into a generic:
|
| 42389 |
maechler |
120 |
|
|
|
121 |
## Since the second argument, 'expr' is used literally, we want
|
|
|
122 |
## with() to only have "data" in the signature.
|
|
|
123 |
|
| 42246 |
jmc |
124 |
\dontrun{
|
|
|
125 |
setGeneric("with", signature = "data")
|
| 42389 |
maechler |
126 |
## Now we could predefine methods for "with" if we wanted to.
|
| 42246 |
jmc |
127 |
|
| 71366 |
jmc |
128 |
## When ready, we store the generic as implicit, and restore the
|
|
|
129 |
original
|
|
|
130 |
|
| 42246 |
jmc |
131 |
setGenericImplicit("with")
|
| 71366 |
jmc |
132 |
}
|
| 42246 |
jmc |
133 |
|
| 71366 |
jmc |
134 |
implicitGeneric("with")
|
| 42246 |
jmc |
135 |
|
| 71366 |
jmc |
136 |
# (This implicit generic is stored in the 'methods' package.)
|
| 42246 |
jmc |
137 |
}
|
|
|
138 |
\keyword{programming}
|
|
|
139 |
\keyword{methods}
|