| Line 9... |
Line 9... |
| 9 |
\alias{prohibitGeneric}
|
9 |
\alias{prohibitGeneric}
|
| 10 |
\alias{registerImplicitGenerics}
|
10 |
\alias{registerImplicitGenerics}
|
| 11 |
\alias{implicit generic}
|
11 |
\alias{implicit generic}
|
| 12 |
\title{Manage Implicit Versions of Generic Functions}
|
12 |
\title{Manage Implicit Versions of Generic Functions}
|
| 13 |
\description{
|
13 |
\description{
|
| 14 |
Create or access implicit generic functions, used to enforce
|
14 |
The implicit generic mechanism stores generic versions of
|
| - |
|
15 |
functions
|
| 15 |
consistent generic versions of functions that are not currently
|
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 |
|
| 16 |
generic. Function \code{implicitGeneric()} returns the implicit
|
27 |
Function \code{implicitGeneric()} returns the implicit
|
| 17 |
generic version, \code{setGenericImplicit()} turns a generic implicit,
|
28 |
generic version, \code{setGenericImplicit()} turns a generic implicit,
|
| 18 |
\code{prohibitGeneric()} prevents your function from being made
|
29 |
\code{prohibitGeneric()} prevents your function from being made
|
| 19 |
generic, and \code{registerImplicitGenerics()} saves a set of implicit
|
30 |
generic, and \code{registerImplicitGenerics()} saves a set of implicit
|
| 20 |
generic definitions in the cached table of the current session.
|
31 |
generic definitions in the cached table of the current session.
|
| 21 |
}
|
32 |
}
|
| Line 28... |
Line 39... |
| 28 |
|
39 |
|
| 29 |
\arguments{
|
40 |
\arguments{
|
| 30 |
\item{name}{ Character string name of the function.}
|
41 |
\item{name}{ Character string name of the function.}
|
| 31 |
\item{where}{ Package or environment in which to register the implicit
|
42 |
\item{where}{ Package or environment in which to register the implicit
|
| 32 |
generics. When using the functions from the top level of your own
|
43 |
generics. When using the functions from the top level of your own
|
| 33 |
package source, this argument can usually be omitted (and should
|
44 |
package source, this argument should be omitted.}
|
| 34 |
be).}
|
- |
|
| 35 |
\item{generic}{ Optionally, the generic function definition to be
|
45 |
\item{generic}{ Obsolete, and likely to be deprecated.}
|
| 36 |
cached, but usually omitted. See Details section.}
|
- |
|
| 37 |
\item{restore}{Should the non-generic version of the function be
|
46 |
\item{restore}{Should the non-generic version of the function be
|
| 38 |
restored after the current.}
|
47 |
restored?.}
|
| 39 |
\item{what}{For \code{registerImplicitGenerics()}, Optional table of
|
48 |
\item{what}{Optional table of
|
| 40 |
the implicit generics to register, but nearly always omitted. See
|
49 |
the implicit generics to register, but nearly always omitted, when
|
| 41 |
Details section.}
|
50 |
it defaults to a standard metadata name.}
|
| 42 |
}
|
51 |
}
|
| 43 |
\details{
|
52 |
\details{
|
| 44 |
Multiple packages may define methods for the same function, using the
|
- |
|
| 45 |
version of a function stored in one package. All these methods should
|
- |
|
| 46 |
be marshaled and dispatched consistently when a user calls the
|
- |
|
| 47 |
function. For consistency, the generic version of the function must
|
- |
|
| 48 |
have a unique definition (the same arguments allowed in methods
|
- |
|
| 49 |
signatures, the same values for optional slots such as the value
|
- |
|
| 50 |
class, and the same standard or non-standard definition of the
|
- |
|
| 51 |
function itself).
|
- |
|
| 52 |
|
- |
|
| 53 |
If the original function is already an S4 generic, there is no
|
- |
|
| 54 |
problem. The implicit generic mechanism enforces consistency when the
|
- |
|
| 55 |
version in the package owning the function is \emph{not} generic. If
|
- |
|
| 56 |
a call to \code{\link{setGeneric}()} attempts to turn a function in
|
- |
|
| 57 |
another package into a generic, the mechanism compares the proposed
|
- |
|
| 58 |
new generic function to the implicit generic version of that
|
- |
|
| 59 |
function. If the two agree, all is well. If not, and if the function
|
- |
|
| 60 |
belongs to another package, then the new generic will not be
|
- |
|
| 61 |
associated with that package. Instead, a warning is issued and a
|
- |
|
| 62 |
separate generic function is created, with its package slot set to the
|
- |
|
| 63 |
current package, not the one that owns the non-generic version of the
|
- |
|
| 64 |
function. The effect is that the new package can still define methods
|
- |
|
| 65 |
for this function, but it will not share the methods in other
|
- |
|
| 66 |
packages, since it is forcing a different definition of the generic
|
- |
|
| 67 |
function.
|
- |
|
| 68 |
|
- |
|
| 69 |
The right way to proceed in nearly all cases is to call
|
- |
|
| 70 |
\code{\link{setGeneric}("foo")}, giving \emph{only} the name of the
|
- |
|
| 71 |
function; this will automatically use the implicit generic version.
|
- |
|
| 72 |
If you don't like that version, the best solution is to convince the
|
- |
|
| 73 |
owner of the other package to agree with you and to insert code to
|
- |
|
| 74 |
define the non-default properties of the function (even if the owner
|
- |
|
| 75 |
does not want \code{foo()} to be a generic by default).
|
- |
|
| 76 |
|
- |
|
| 77 |
For any function, the implicit generic form is a standard generic in
|
- |
|
| 78 |
which all formal arguments, except for \code{\dots}, are allowed in
|
- |
|
| 79 |
the signature of methods. If that is the suitable generic for a
|
- |
|
| 80 |
function, no action is needed. If not, the best mechanism is to set up
|
- |
|
| 81 |
the generic in the code of the package owning the function, and to
|
- |
|
| 82 |
then call \code{setGenericImplicit()} to record the implicit generic
|
- |
|
| 83 |
and restore the non-generic version. See the example.
|
- |
|
| 84 |
|
53 |
|
| - |
|
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.
|
| - |
|
61 |
|
| - |
|
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 |
|
| - |
|
70 |
|
| 85 |
Note that the package can define methods for the implicit generic as
|
71 |
Note that the package containing the function can define methods for the implicit generic as
|
| 86 |
well; when the implicit generic is made a real generic, those methods
|
72 |
well; when the implicit generic is made a real generic, those methods
|
| 87 |
will be included.
|
73 |
will be included.
|
| 88 |
|
74 |
|
| 89 |
Other than predefining methods, the usual reason for having a
|
75 |
The usual reason for having a
|
| 90 |
non-default implicit generic is to provide a non-default signature,
|
76 |
non-default implicit generic is to provide a non-default signature,
|
| 91 |
and the usual reason for \emph{that} is to allow lazy evaluation of
|
77 |
and the usual reason for \emph{that} is to allow lazy evaluation of
|
| 92 |
some arguments. See the example. All arguments in the signature of a
|
78 |
some arguments. All arguments in the signature of a
|
| 93 |
generic function must be evaluated at the time the function needs to
|
79 |
generic function must be evaluated at the time the function needs to
|
| 94 |
select a method. (But those arguments can be missing, with or without
|
80 |
select a method.
|
| 95 |
a default expression being defined; you can always examine
|
81 |
In the base function \code{with()} in the example below, evaluation of the argument
|
| 96 |
\code{missing(x)} even for arguments in the signature.)
|
82 |
\code{expr} must be delayed; therefore, it is excluded from the signature.
|
| 97 |
|
83 |
|
| 98 |
If you want to completely prohibit anyone from turning your function
|
84 |
If you want to completely prohibit anyone from turning your function
|
| 99 |
into a generic, call \code{prohibitGeneric()}.
|
85 |
into a generic, call \code{prohibitGeneric()}.
|
| - |
|
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.
|
| - |
|
91 |
}
|
| - |
|
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.
|
| 100 |
}
|
105 |
}
|
| - |
|
106 |
|
| 101 |
\value{
|
107 |
\value{
|
| 102 |
Function \code{implicitGeneric()} returns the implicit generic
|
108 |
Function \code{implicitGeneric()} returns the implicit generic
|
| 103 |
definition (and caches that definition the first time if it has to
|
109 |
definition (and caches that definition the first time if it has to
|
| 104 |
construct it).
|
110 |
construct it).
|
| 105 |
|
111 |
|
| 106 |
The other functions exist for their side effect and return nothing
|
112 |
The other functions exist for their side effect and return nothing
|
| 107 |
useful.
|
113 |
useful.
|
| 108 |
}
|
114 |
}
|
| - |
|
115 |
|
| 109 |
\seealso{\code{\link{setGeneric}}}
|
116 |
\seealso{\code{\link{setGeneric}}}
|
| 110 |
\examples{
|
117 |
\examples{
|
| 111 |
|
118 |
|
| 112 |
### How we would make the function \link{with}() into a generic:
|
119 |
### How we would make the function with() into a generic:
|
| 113 |
|
120 |
|
| 114 |
## Since the second argument, 'expr' is used literally, we want
|
121 |
## Since the second argument, 'expr' is used literally, we want
|
| 115 |
## with() to only have "data" in the signature.
|
122 |
## with() to only have "data" in the signature.
|
| 116 |
|
123 |
|
| 117 |
## Note that 'methods'-internal code now has already extended with()
|
- |
|
| 118 |
## to do the equivalent of the following
|
- |
|
| 119 |
\dontrun{
|
124 |
\dontrun{
|
| 120 |
setGeneric("with", signature = "data")
|
125 |
setGeneric("with", signature = "data")
|
| 121 |
## Now we could predefine methods for "with" if we wanted to.
|
126 |
## Now we could predefine methods for "with" if we wanted to.
|
| 122 |
|
127 |
|
| 123 |
## When ready, we store the generic as implicit, and restore the original
|
128 |
## When ready, we store the generic as implicit, and restore the
|
| 124 |
setGenericImplicit("with")
|
129 |
original
|
| 125 |
|
130 |
|
| 126 |
## (This example would only work if we "owned" function with(),
|
- |
|
| 127 |
## but it is in base.)}
|
131 |
setGenericImplicit("with")
|
| - |
|
132 |
}
|
| 128 |
|
133 |
|
| 129 |
implicitGeneric("with")
|
134 |
implicitGeneric("with")
|
| 130 |
}
|
- |
|
| 131 |
|
135 |
|
| - |
|
136 |
# (This implicit generic is stored in the 'methods' package.)
|
| - |
|
137 |
}
|
| 132 |
\keyword{programming}
|
138 |
\keyword{programming}
|
| 133 |
\keyword{methods}
|
139 |
\keyword{methods}
|