| 42333 |
ripley |
1 |
% File src/library/methods/man/RMethodUtils.Rd
|
|
|
2 |
% Part of the R package, http://www.R-project.org
|
| 59039 |
ripley |
3 |
% Copyright 1995-2008 R Core Team
|
| 42333 |
ripley |
4 |
% Distributed under GPL 2 or later
|
|
|
5 |
|
| 15357 |
jmc |
6 |
\name{RMethodUtils}
|
| 21345 |
jmc |
7 |
%% FIXME: undocumented utilities (Sep. 23/02)
|
|
|
8 |
\alias{asMethodDefinition}
|
| 26093 |
jmc |
9 |
\alias{standardGeneric-class}
|
|
|
10 |
\alias{standardGenericWithTrace-class}
|
| 21345 |
jmc |
11 |
\alias{nonstandardGeneric-class}
|
|
|
12 |
\alias{nonstandardGenericFunction-class}
|
|
|
13 |
\alias{nonstandardGroupGenericFunction-class}
|
|
|
14 |
\alias{OptionalFunction-class}
|
|
|
15 |
\alias{PossibleMethod-class}
|
| 51427 |
jmc |
16 |
\alias{optionalMethod-class}
|
| 25816 |
jmc |
17 |
\alias{derivedDefaultMethod-class}
|
| 21345 |
jmc |
18 |
%%
|
|
|
19 |
\alias{substituteFunctionArgs}
|
| 15357 |
jmc |
20 |
\alias{makeGeneric}
|
| 15431 |
jmc |
21 |
\alias{makeStandardGeneric}
|
|
|
22 |
\alias{generic.skeleton}
|
| 15357 |
jmc |
23 |
\alias{defaultDumpName}
|
| 15431 |
jmc |
24 |
\alias{doPrimitiveMethod}
|
| 17000 |
jmc |
25 |
\alias{conformMethod}
|
| 15431 |
jmc |
26 |
\alias{getGeneric}
|
|
|
27 |
\alias{getGroup}
|
| 31621 |
jmc |
28 |
\alias{getGroupMembers}
|
| 16302 |
jmc |
29 |
\alias{getMethodsMetaData}
|
|
|
30 |
\alias{assignMethodsMetaData}
|
| 16471 |
jmc |
31 |
\alias{matchSignature}
|
| 17454 |
jmc |
32 |
\alias{findUnique}
|
|
|
33 |
\alias{MethodAddCoerce}
|
| 16471 |
jmc |
34 |
\alias{.saveImage}
|
| 16764 |
jmc |
35 |
\alias{cacheMetaData}
|
|
|
36 |
\alias{cacheGenericsMetaData}
|
| 16802 |
jmc |
37 |
\alias{setPrimitiveMethods}
|
| 18364 |
jmc |
38 |
\alias{missingArg}
|
| 18413 |
jmc |
39 |
\alias{balanceMethodsList}
|
| 18558 |
jmc |
40 |
\alias{sigToEnv}
|
| 19064 |
jmc |
41 |
\alias{rematchDefinition}
|
|
|
42 |
\alias{unRematchDefinition}
|
| 25816 |
jmc |
43 |
\alias{addNextMethod,MethodDefinition-method}
|
|
|
44 |
\alias{addNextMethod,MethodWithNext-method}
|
| 23907 |
jmc |
45 |
\alias{addNextMethod}
|
| 29354 |
ripley |
46 |
\alias{.valueClassTest}
|
| 59566 |
jmc |
47 |
\alias{insertClassMethods}
|
| 15822 |
jmc |
48 |
%% Not for the user to know about!
|
|
|
49 |
\alias{.ShortPrimitiveSkeletons} %% used from C code
|
|
|
50 |
\alias{.EmptyPrimitiveSkeletons}
|
| 44920 |
ripley |
51 |
\title{Method Utilities}
|
| 18994 |
hornik |
52 |
\description{
|
|
|
53 |
Utility functions to support the definition and use of formal
|
|
|
54 |
methods. Most of these functions will not normally be called directly
|
|
|
55 |
by the user.
|
|
|
56 |
}
|
| 15357 |
jmc |
57 |
\usage{
|
| 39087 |
ripley |
58 |
getGeneric(f, mustFind=FALSE, where, package)
|
| 25902 |
jmc |
59 |
|
|
|
60 |
getGroup(fdef, recursive, where)
|
| 31845 |
ripley |
61 |
getGroupMembers(group, recursive = FALSE, character = TRUE)
|
| 25902 |
jmc |
62 |
|
|
|
63 |
getMethodsMetaData(f, where)
|
|
|
64 |
assignMethodsMetaData (f, value, fdef, where, deflt)
|
|
|
65 |
|
| 46546 |
jmc |
66 |
makeGeneric(f, fdef, fdefault =, group=list(), valueClass=character(),
|
|
|
67 |
package =, signature = NULL, genericFunction = NULL,
|
|
|
68 |
simpleInheritanceOnly = NULL)
|
| 15357 |
jmc |
69 |
|
| 15431 |
jmc |
70 |
makeStandardGeneric(f, fdef)
|
|
|
71 |
|
|
|
72 |
generic.skeleton(name, fdef, fdefault)
|
|
|
73 |
|
| 15357 |
jmc |
74 |
defaultDumpName(generic, signature)
|
|
|
75 |
|
| 39087 |
ripley |
76 |
doPrimitiveMethod(name, def, call= sys.call(sys.parent()),
|
|
|
77 |
ev = sys.frame(sys.parent(2)))
|
| 15357 |
jmc |
78 |
|
| 44864 |
jmc |
79 |
conformMethod(signature, mnames, fnames, f= , fdef, method)
|
| 15431 |
jmc |
80 |
|
| 25852 |
jmc |
81 |
matchSignature(signature, fun, where)
|
| 16764 |
jmc |
82 |
|
| 25816 |
jmc |
83 |
findUnique(what, message, where)
|
| 17454 |
jmc |
84 |
|
|
|
85 |
MethodAddCoerce(method, argName, thisClass, methodClass)
|
|
|
86 |
|
| 62209 |
hornik |
87 |
cacheMetaData(where, attach = TRUE, searchWhere = as.environment(where),
|
|
|
88 |
doCheck = TRUE)
|
| 16802 |
jmc |
89 |
|
| 25816 |
jmc |
90 |
cacheGenericsMetaData(f, fdef, attach = TRUE, where, package, methods)
|
| 16764 |
jmc |
91 |
|
| 16802 |
jmc |
92 |
setPrimitiveMethods(f, fdef, code, generic, mlist)
|
| 16764 |
jmc |
93 |
|
| 20092 |
jmc |
94 |
missingArg(symbol, envir = parent.frame(), eval)
|
| 18364 |
jmc |
95 |
|
| 18413 |
jmc |
96 |
balanceMethodsList(mlist, args, check = TRUE)
|
|
|
97 |
|
| 30842 |
jmc |
98 |
sigToEnv(signature, generic)
|
| 18558 |
jmc |
99 |
|
| 22886 |
jmc |
100 |
rematchDefinition(definition, generic, mnames, fnames, signature)
|
| 19064 |
jmc |
101 |
unRematchDefinition(definition)
|
|
|
102 |
|
| 46394 |
maechler |
103 |
asMethodDefinition(def, signature, sealed = FALSE, fdef)
|
| 20092 |
jmc |
104 |
|
| 23907 |
jmc |
105 |
addNextMethod(method, f, mlist, optional, envir)
|
| 59566 |
jmc |
106 |
|
|
|
107 |
insertClassMethods(methods, Class, value, fieldNames, returnAll)
|
| 15357 |
jmc |
108 |
}
|
| 18994 |
hornik |
109 |
\section{Summary of Functions}{
|
| 26530 |
maechler |
110 |
|
| 18994 |
hornik |
111 |
\describe{
|
| 26530 |
maechler |
112 |
|
|
|
113 |
\item{\code{getGeneric}:}{
|
| 31621 |
jmc |
114 |
returns the definition of the function named \code{f} as a generic.
|
| 15431 |
jmc |
115 |
|
| 25351 |
jmc |
116 |
If no definition is found, throws an
|
| 23443 |
hornik |
117 |
error or returns \code{NULL} according to the value of
|
| 25351 |
jmc |
118 |
\code{mustFind}. By default, searches in the top-level
|
|
|
119 |
environment (normally the global environment, but adjusted to
|
| 28314 |
hornik |
120 |
work correctly when package code is evaluated from the function
|
|
|
121 |
\code{\link{library}}).
|
| 15431 |
jmc |
122 |
|
| 18994 |
hornik |
123 |
Primitive functions are dealt with specially, since there is never
|
|
|
124 |
a formal generic definition for them. The value returned is the
|
|
|
125 |
formal definition used for assigning methods to this primitive.
|
|
|
126 |
Not all primitives can have methods; if this one can't, then
|
|
|
127 |
\code{getGeneric} returns \code{NULL} or throws an error.
|
|
|
128 |
}
|
| 15357 |
jmc |
129 |
|
| 26530 |
maechler |
130 |
\item{\code{getGroup}:}{
|
| 31621 |
jmc |
131 |
returns the groups to which this generic belongs, searching from
|
| 25816 |
jmc |
132 |
environment \code{where} (the global environment normally by default).
|
| 15431 |
jmc |
133 |
|
| 18994 |
hornik |
134 |
If \code{recursive=TRUE}, also all the group(s) of these groups.
|
|
|
135 |
}
|
| 16707 |
jmc |
136 |
|
| 31621 |
jmc |
137 |
\item{\code{getGroupMembers}:}{
|
|
|
138 |
Return all the members of the group generic function named
|
|
|
139 |
\code{group}.
|
|
|
140 |
If \code{recursive} is \code{TRUE}, and some members are group
|
|
|
141 |
generics, includes their members as well.
|
|
|
142 |
If \code{character} is \code{TRUE}, returns just a character
|
|
|
143 |
vector of the names; otherwise returns a list, whose elements may
|
|
|
144 |
(or may not) include either names with a package attribute or
|
|
|
145 |
actual generic functions.
|
|
|
146 |
|
|
|
147 |
Note that members that are not defined as generic functions will
|
| 33631 |
maechler |
148 |
\emph{not} be included in the returned value. To see the raw data,
|
|
|
149 |
use \code{getGeneric(group)@groupMembers}.
|
| 31621 |
jmc |
150 |
}
|
|
|
151 |
|
| 25902 |
jmc |
152 |
\item{\code{getMethodsMetaData},
|
|
|
153 |
\code{assignMethodsMetaData},
|
| 33631 |
maechler |
154 |
\code{mlistMetaName}:}{Utilities to get
|
|
|
155 |
(\code{getMethodsMetaData}) and assign
|
|
|
156 |
(\code{assignMethodsMetaData}) the metadata object recording the
|
|
|
157 |
methods defined in a particular package, or to return the mangled
|
|
|
158 |
name for that object (\code{mlistMetaName}).
|
| 25902 |
jmc |
159 |
|
|
|
160 |
The assign function should not be used directly. The get
|
| 33631 |
maechler |
161 |
function may be useful if you want explicitly only the outcome of
|
|
|
162 |
the methods assigned in this package. Otherwise, use
|
|
|
163 |
\code{\link{getMethods}}.
|
| 25902 |
jmc |
164 |
}
|
|
|
165 |
|
| 23443 |
hornik |
166 |
\item{\code{matchSignature}:}{
|
| 18994 |
hornik |
167 |
Matches the signature object (a partially or completely named
|
| 20092 |
jmc |
168 |
subset of the signature arguments of the generic function object
|
| 23443 |
hornik |
169 |
\code{fun}), and return a vector of all the classes in the order
|
|
|
170 |
specified by \code{fun@signature}. The classes not specified by
|
|
|
171 |
\code{signature} will be \code{"ANY"} in the value, but extra
|
|
|
172 |
trailing \code{"ANY"}'s are removed. When the input signature is
|
|
|
173 |
empty, the returned signature is a single \code{"ANY"} matching
|
|
|
174 |
the first formal argument (so the returned value is always
|
|
|
175 |
non-empty).
|
| 26530 |
maechler |
176 |
|
| 20092 |
jmc |
177 |
Generates an error if any of the supplied signature names are
|
| 23443 |
hornik |
178 |
not legal; that is, not in the signature slot of the generic
|
|
|
179 |
function.
|
| 25852 |
jmc |
180 |
|
|
|
181 |
If argument \code{where} is supplied, a warning will be issued
|
|
|
182 |
if any of the classes does not have a formal definition visible
|
|
|
183 |
from \code{where}.
|
| 18994 |
hornik |
184 |
}
|
| 16471 |
jmc |
185 |
|
| 23443 |
hornik |
186 |
\item{\code{MethodAddCoerce}:}{
|
| 17454 |
jmc |
187 |
Possibly modify one or more methods to explicitly coerce this
|
|
|
188 |
argument to \code{methodClass}, the class for which the method is
|
|
|
189 |
explicitly defined. Only modifies the method if an explicit
|
| 18994 |
hornik |
190 |
coerce is required to coerce from \code{thisClass} to
|
|
|
191 |
\code{methodClass}.
|
| 17454 |
jmc |
192 |
}
|
| 26530 |
maechler |
193 |
|
| 18994 |
hornik |
194 |
\item{\code{findUnique}:}{
|
| 25816 |
jmc |
195 |
Return the list of environments (or equivalent) having an object named
|
|
|
196 |
\code{what}, using environment \code{where} and its parent
|
|
|
197 |
environments. If more than one is found, a warning message is
|
| 18994 |
hornik |
198 |
generated, using \code{message} to identify what was being
|
| 25816 |
jmc |
199 |
searched for, unless \code{message} is the empty string.
|
| 18994 |
hornik |
200 |
}
|
| 26530 |
maechler |
201 |
|
| 23443 |
hornik |
202 |
\item{\code{cacheMetaData},
|
|
|
203 |
\code{cacheGenericsMetaData},
|
|
|
204 |
\code{setPrimitiveMethods}:}{
|
| 25902 |
jmc |
205 |
Utilities for ensuring that the internal information about
|
| 16764 |
jmc |
206 |
class and method definitions is up to date. Should normally be
|
|
|
207 |
called automatically whenever needed (for example, when a method
|
| 19087 |
jmc |
208 |
or class definition changes, or when a package is attached or
|
| 25902 |
jmc |
209 |
detached). Required primarily because primitive functions are
|
|
|
210 |
dispatched in C code, rather than by the official model.
|
| 16764 |
jmc |
211 |
|
| 16802 |
jmc |
212 |
The \code{setPrimitiveMethods} function resets the caching
|
| 18994 |
hornik |
213 |
information for a particular primitive function. Don't call it
|
|
|
214 |
directly.
|
|
|
215 |
}
|
| 16471 |
jmc |
216 |
|
| 23443 |
hornik |
217 |
\item{\code{missingArg}:}{
|
| 18994 |
hornik |
218 |
Returns \code{TRUE} if the symbol supplied is missing \emph{from
|
| 19087 |
jmc |
219 |
the call} corresponding to the environment supplied (by default,
|
| 18994 |
hornik |
220 |
environment of the call to \code{missingArg}). If \code{eval} is
|
|
|
221 |
true, the argument is evaluated to get the name of the symbol to
|
| 25118 |
hornik |
222 |
test. Note that \code{missingArg} is closer to the
|
| 42963 |
ripley |
223 |
\sQuote{Blue Book} sense of the \code{\link{missing}} function,
|
| 25118 |
hornik |
224 |
not that of the current R base package implementation. But beware
|
|
|
225 |
that it works reliably only if no assignment has yet been made to
|
|
|
226 |
the argument. (For method dispatch this is fine, because
|
| 40284 |
ripley |
227 |
computations are done at the beginning of the call.)
|
| 18994 |
hornik |
228 |
}
|
|
|
229 |
|
| 23443 |
hornik |
230 |
\item{\code{balanceMethodsList}:}{
|
| 18994 |
hornik |
231 |
Called from \code{\link{setMethod}} to ensure that all nodes in
|
|
|
232 |
the list have the same depth (i.e., the same number of levels of
|
|
|
233 |
arguments). Balance is needed to ensure that all necessary
|
|
|
234 |
arguments are examined when inherited methods are being found and
|
|
|
235 |
added to the \code{allMethods} slot. No actual recomputation is
|
|
|
236 |
needed usually except when a new method uses a longer signature
|
|
|
237 |
than has appeared before.
|
|
|
238 |
|
|
|
239 |
Balance requires that \emph{all} methods be added to the generic
|
|
|
240 |
via \code{setMethod} (how else could you do it?) or by the initial
|
|
|
241 |
\code{setGeneric} call converting the ordinary function.
|
|
|
242 |
}
|
|
|
243 |
|
| 23443 |
hornik |
244 |
\item{\code{sigToEnv}:}{
|
| 18994 |
hornik |
245 |
Turn the signature (a named vector of classes) into an environment
|
|
|
246 |
with the classes assigned to the names. The environment is then
|
|
|
247 |
suitable for calling \code{\link{MethodsListSelect}}, with
|
|
|
248 |
\code{evalArgs=FALSE}, to select a method corresponding to the
|
|
|
249 |
signature. Usually not called directly: see
|
|
|
250 |
\code{\link{selectMethod}}.
|
|
|
251 |
}
|
| 26530 |
maechler |
252 |
|
| 23443 |
hornik |
253 |
\item{\code{.saveImage}:}{
|
|
|
254 |
Flag, used in dynamically initializing the methods package from
|
| 56382 |
murdoch |
255 |
\code{.onLoad}.
|
| 23443 |
hornik |
256 |
}
|
|
|
257 |
|
|
|
258 |
\item{\code{rematchDefinition},
|
|
|
259 |
\code{unRematchDefinition}:}{
|
|
|
260 |
If the specified method in a call to \code{\link{setMethod}}
|
|
|
261 |
specializes the argument list (by replacing \dots), then
|
|
|
262 |
\code{rematchDefinition} constructs the actual method stored.
|
|
|
263 |
Using knowledge of how \code{rematchDefinition} works,
|
|
|
264 |
\code{unRematchDefinition} reverses the procedure; if given a
|
|
|
265 |
function or method definition that does not correspond to this
|
|
|
266 |
form, it just returns its argument.
|
|
|
267 |
}
|
|
|
268 |
|
|
|
269 |
\item{\code{asMethodDefinition}:}{
|
| 44751 |
maechler |
270 |
Turn a function definition into an object of class
|
|
|
271 |
\code{\linkS4class{MethodDefinition}}, corresponding to the
|
| 20092 |
jmc |
272 |
given \code{signature} (by default generates a default method
|
| 23493 |
jmc |
273 |
with empty signature). The definition is sealed according to
|
|
|
274 |
the \code{sealed} argument.
|
| 23443 |
hornik |
275 |
}
|
|
|
276 |
|
| 23907 |
jmc |
277 |
\item{\code{addNextMethod}:}{
|
| 44805 |
jmc |
278 |
A generic function that finds the next method for
|
|
|
279 |
the signature of the method definition \code{method} and caches
|
|
|
280 |
that method in the method definition (promoting the class to
|
|
|
281 |
\code{"MethodWithNext"}. Note that argument \code{mlist} is
|
|
|
282 |
obsolete and not used.
|
| 23443 |
hornik |
283 |
}
|
| 26530 |
maechler |
284 |
|
|
|
285 |
\item{\code{makeGeneric}:}{
|
| 25902 |
jmc |
286 |
Makes a generic function object corresponding to the given
|
|
|
287 |
function name, optional definition and optional default method.
|
| 44751 |
maechler |
288 |
Other arguments supply optional elements for the slots of class
|
|
|
289 |
\code{\linkS4class{genericFunction}}.
|
| 25902 |
jmc |
290 |
}
|
|
|
291 |
|
| 26530 |
maechler |
292 |
\item{\code{makeStandardGeneric}:}{
|
| 25902 |
jmc |
293 |
a utility function that makes a valid function calling
|
|
|
294 |
\code{standardGeneric} for name \code{f}. Works (more or less)
|
|
|
295 |
even if the actual definition, \code{fdef}, is not a proper
|
|
|
296 |
function, that is, it's a primitive or internal.
|
|
|
297 |
}
|
| 26530 |
maechler |
298 |
|
| 25902 |
jmc |
299 |
\item{\code{conformMethod}:}{
|
|
|
300 |
If the formal arguments, \code{mnames}, are not identical to the
|
|
|
301 |
formal arguments to the function, \code{fnames},
|
|
|
302 |
\code{conformMethod} determines whether the signature and the two
|
|
|
303 |
sets of arguments conform, and returns the signature, possibly
|
|
|
304 |
extended. The function name, \code{f} is supplied for error
|
| 42839 |
maechler |
305 |
messages. The generic function, \code{fdef}, supplies the
|
|
|
306 |
generic signature for matching purposes.
|
| 25902 |
jmc |
307 |
|
| 50687 |
jmc |
308 |
The method assignment conforms if method and generic function have
|
| 25902 |
jmc |
309 |
identical formal argument lists. It can also conform if the
|
|
|
310 |
method omits some of the formal arguments of the function but: (1)
|
|
|
311 |
the non-omitted arguments are a subset of the function arguments,
|
|
|
312 |
appearing in the same order; (2) there are no arguments to the
|
| 50687 |
jmc |
313 |
method that are not arguments to the function; and (3) the omitted
|
| 25902 |
jmc |
314 |
formal arguments do not appear as explicit classes in the
|
| 50687 |
jmc |
315 |
signature. A future extension hopes to test also that the
|
| 61433 |
ripley |
316 |
omitted arguments are not assumed by being used as locally assigned
|
| 44864 |
jmc |
317 |
names or function names in the body of the method.
|
| 25902 |
jmc |
318 |
}
|
|
|
319 |
|
| 26530 |
maechler |
320 |
\item{\code{defaultDumpName}:}{
|
| 25902 |
jmc |
321 |
the default name to be used for dumping a method.
|
|
|
322 |
}
|
| 26530 |
maechler |
323 |
|
| 25902 |
jmc |
324 |
|
| 26530 |
maechler |
325 |
\item{\code{doPrimitiveMethod}:}{
|
| 25902 |
jmc |
326 |
do a primitive call to builtin function \code{name} the definition
|
|
|
327 |
and call provided, and carried out in the environment \code{ev}.
|
|
|
328 |
|
|
|
329 |
A call to \code{doPrimitiveMethod} is used when the actual method
|
|
|
330 |
is a .Primitive. (Because primitives don't behave correctly as
|
|
|
331 |
ordinary functions, not having either formal arguments nor a
|
|
|
332 |
function body).
|
|
|
333 |
}
|
|
|
334 |
|
| 28314 |
hornik |
335 |
}
|
| 19064 |
jmc |
336 |
}
|
| 33631 |
maechler |
337 |
\seealso{\code{\link{setGeneric}}, \code{\link{setClass}},
|
|
|
338 |
\code{\link{showMethods}}.
|
|
|
339 |
}
|
|
|
340 |
\examples{
|
|
|
341 |
getGroup("exp")
|
|
|
342 |
getGroup("==", recursive = TRUE)
|
|
|
343 |
|
|
|
344 |
getGroupMembers("Arith")
|
|
|
345 |
getGroupMembers("Math")
|
| 39651 |
maechler |
346 |
getGroupMembers("Ops") # -> its sub groups
|
| 33631 |
maechler |
347 |
}
|
| 16471 |
jmc |
348 |
\keyword{internal}
|