| 42333 |
ripley |
1 |
% File src/library/methods/man/RMethodUtils.Rd
|
| 68948 |
ripley |
2 |
% Part of the R package, https://www.R-project.org
|
| 86513 |
maechler |
3 |
% Copyright 1995-2024 R Core Team
|
| 42333 |
ripley |
4 |
% Distributed under GPL 2 or later
|
|
|
5 |
|
| 15357 |
jmc |
6 |
\name{RMethodUtils}
|
| 68104 |
maechler |
7 |
\title{Method Utilities}
|
| 21345 |
jmc |
8 |
%%
|
|
|
9 |
\alias{substituteFunctionArgs}
|
| 15357 |
jmc |
10 |
\alias{makeGeneric}
|
| 15431 |
jmc |
11 |
\alias{makeStandardGeneric}
|
|
|
12 |
\alias{generic.skeleton}
|
| 15357 |
jmc |
13 |
\alias{defaultDumpName}
|
| 15431 |
jmc |
14 |
\alias{doPrimitiveMethod}
|
| 17000 |
jmc |
15 |
\alias{conformMethod}
|
| 15431 |
jmc |
16 |
\alias{getGeneric}
|
|
|
17 |
\alias{getGroup}
|
| 31621 |
jmc |
18 |
\alias{getGroupMembers}
|
| 16302 |
jmc |
19 |
\alias{getMethodsMetaData}
|
|
|
20 |
\alias{assignMethodsMetaData}
|
| 16471 |
jmc |
21 |
\alias{matchSignature}
|
| 17454 |
jmc |
22 |
\alias{findUnique}
|
|
|
23 |
\alias{MethodAddCoerce}
|
| 16471 |
jmc |
24 |
\alias{.saveImage}
|
| 16764 |
jmc |
25 |
\alias{cacheMetaData}
|
|
|
26 |
\alias{cacheGenericsMetaData}
|
| 16802 |
jmc |
27 |
\alias{setPrimitiveMethods}
|
| 18364 |
jmc |
28 |
\alias{missingArg}
|
| 86513 |
maechler |
29 |
\alias{balanceMethodsList}% defunct
|
| 18558 |
jmc |
30 |
\alias{sigToEnv}
|
| 19064 |
jmc |
31 |
\alias{rematchDefinition}
|
| 70405 |
lawrence |
32 |
\alias{isRematched}
|
| 19064 |
jmc |
33 |
\alias{unRematchDefinition}
|
| 86513 |
maechler |
34 |
\alias{asMethodDefinition}
|
| 25816 |
jmc |
35 |
\alias{addNextMethod,MethodDefinition-method}
|
|
|
36 |
\alias{addNextMethod,MethodWithNext-method}
|
| 23907 |
jmc |
37 |
\alias{addNextMethod}
|
| 29354 |
ripley |
38 |
\alias{.valueClassTest}
|
| 59566 |
jmc |
39 |
\alias{insertClassMethods}
|
| 15822 |
jmc |
40 |
%% Not for the user to know about!
|
|
|
41 |
\alias{.ShortPrimitiveSkeletons} %% used from C code
|
|
|
42 |
\alias{.EmptyPrimitiveSkeletons}
|
| 18994 |
hornik |
43 |
\description{
|
|
|
44 |
Utility functions to support the definition and use of formal
|
|
|
45 |
methods. Most of these functions will not normally be called directly
|
|
|
46 |
by the user.
|
| 86513 |
maechler |
47 |
|
|
|
48 |
Partly, they work with S4 classes which are also normally not for the
|
| 86516 |
maechler |
49 |
user, see their help page \code{\linkS4class{optionalMethod}}.
|
| 18994 |
hornik |
50 |
}
|
| 15357 |
jmc |
51 |
\usage{
|
| 39087 |
ripley |
52 |
getGeneric(f, mustFind=FALSE, where, package)
|
| 25902 |
jmc |
53 |
|
|
|
54 |
getGroup(fdef, recursive, where)
|
| 31845 |
ripley |
55 |
getGroupMembers(group, recursive = FALSE, character = TRUE)
|
| 25902 |
jmc |
56 |
|
|
|
57 |
getMethodsMetaData(f, where)
|
| 69639 |
lawrence |
58 |
assignMethodsMetaData (f, value, fdef, where)
|
| 25902 |
jmc |
59 |
|
| 46546 |
jmc |
60 |
makeGeneric(f, fdef, fdefault =, group=list(), valueClass=character(),
|
|
|
61 |
package =, signature = NULL, genericFunction = NULL,
|
|
|
62 |
simpleInheritanceOnly = NULL)
|
| 15357 |
jmc |
63 |
|
| 15431 |
jmc |
64 |
makeStandardGeneric(f, fdef)
|
|
|
65 |
|
|
|
66 |
generic.skeleton(name, fdef, fdefault)
|
|
|
67 |
|
| 15357 |
jmc |
68 |
defaultDumpName(generic, signature)
|
|
|
69 |
|
| 39087 |
ripley |
70 |
doPrimitiveMethod(name, def, call= sys.call(sys.parent()),
|
|
|
71 |
ev = sys.frame(sys.parent(2)))
|
| 15357 |
jmc |
72 |
|
| 44864 |
jmc |
73 |
conformMethod(signature, mnames, fnames, f= , fdef, method)
|
| 15431 |
jmc |
74 |
|
| 25852 |
jmc |
75 |
matchSignature(signature, fun, where)
|
| 16764 |
jmc |
76 |
|
| 25816 |
jmc |
77 |
findUnique(what, message, where)
|
| 17454 |
jmc |
78 |
|
|
|
79 |
MethodAddCoerce(method, argName, thisClass, methodClass)
|
|
|
80 |
|
| 62209 |
hornik |
81 |
cacheMetaData(where, attach = TRUE, searchWhere = as.environment(where),
|
|
|
82 |
doCheck = TRUE)
|
| 16802 |
jmc |
83 |
|
| 25816 |
jmc |
84 |
cacheGenericsMetaData(f, fdef, attach = TRUE, where, package, methods)
|
| 16764 |
jmc |
85 |
|
| 16802 |
jmc |
86 |
setPrimitiveMethods(f, fdef, code, generic, mlist)
|
| 16764 |
jmc |
87 |
|
| 20092 |
jmc |
88 |
missingArg(symbol, envir = parent.frame(), eval)
|
| 18364 |
jmc |
89 |
|
| 30842 |
jmc |
90 |
sigToEnv(signature, generic)
|
| 18558 |
jmc |
91 |
|
| 22886 |
jmc |
92 |
rematchDefinition(definition, generic, mnames, fnames, signature)
|
| 19064 |
jmc |
93 |
unRematchDefinition(definition)
|
| 70405 |
lawrence |
94 |
isRematched(definition)
|
| 19064 |
jmc |
95 |
|
| 46394 |
maechler |
96 |
asMethodDefinition(def, signature, sealed = FALSE, fdef)
|
| 20092 |
jmc |
97 |
|
| 23907 |
jmc |
98 |
addNextMethod(method, f, mlist, optional, envir)
|
| 59566 |
jmc |
99 |
|
|
|
100 |
insertClassMethods(methods, Class, value, fieldNames, returnAll)
|
| 68104 |
maechler |
101 |
|
| 86513 |
maechler |
102 |
balanceMethodsList(mlist, args, check = TRUE) # <- defunct since R 4.5.0
|
| 83769 |
hornik |
103 |
|
|
|
104 |
substituteFunctionArgs(def, newArgs, args = formalArgs(def),
|
| 86516 |
maechler |
105 |
silent = FALSE, functionName = "a function")
|
| 83769 |
hornik |
106 |
|
|
|
107 |
.valueClassTest(object, classes, fname)
|
| 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}:}{
|
| 86513 |
maechler |
231 |
Used to be called from \code{setMethod()} and is \emph{defunct} since
|
|
|
232 |
\R version 4.5.0 (\sQuote{deprecated} \R 3.2.0).
|
| 68104 |
maechler |
233 |
%% Called from \code{\link{setMethod}} to ensure that all nodes in
|
|
|
234 |
%% the list have the same depth (i.e., the same number of levels of
|
|
|
235 |
%% arguments). Balance is needed to ensure that all necessary
|
|
|
236 |
%% arguments are examined when inherited methods are being found and
|
|
|
237 |
%% added to the \code{allMethods} slot. No actual recomputation is
|
|
|
238 |
%% needed usually except when a new method uses a longer signature
|
|
|
239 |
%% than has appeared before.
|
| 18994 |
hornik |
240 |
|
| 68104 |
maechler |
241 |
%% Balance requires that \emph{all} methods be added to the generic
|
|
|
242 |
%% via \code{setMethod} (how else could you do it?) or by the initial
|
|
|
243 |
%% \code{setGeneric} call converting the ordinary function.
|
| 18994 |
hornik |
244 |
}
|
|
|
245 |
|
| 23443 |
hornik |
246 |
\item{\code{sigToEnv}:}{
|
| 18994 |
hornik |
247 |
Turn the signature (a named vector of classes) into an environment
|
|
|
248 |
with the classes assigned to the names. The environment is then
|
|
|
249 |
suitable for calling \code{\link{MethodsListSelect}}, with
|
|
|
250 |
\code{evalArgs=FALSE}, to select a method corresponding to the
|
|
|
251 |
signature. Usually not called directly: see
|
|
|
252 |
\code{\link{selectMethod}}.
|
|
|
253 |
}
|
| 26530 |
maechler |
254 |
|
| 23443 |
hornik |
255 |
\item{\code{.saveImage}:}{
|
|
|
256 |
Flag, used in dynamically initializing the methods package from
|
| 56382 |
murdoch |
257 |
\code{.onLoad}.
|
| 23443 |
hornik |
258 |
}
|
|
|
259 |
|
|
|
260 |
\item{\code{rematchDefinition},
|
| 70405 |
lawrence |
261 |
\code{unRematchDefinition},
|
|
|
262 |
\code{isRematched}:}{
|
| 23443 |
hornik |
263 |
If the specified method in a call to \code{\link{setMethod}}
|
|
|
264 |
specializes the argument list (by replacing \dots), then
|
|
|
265 |
\code{rematchDefinition} constructs the actual method stored.
|
|
|
266 |
Using knowledge of how \code{rematchDefinition} works,
|
|
|
267 |
\code{unRematchDefinition} reverses the procedure; if given a
|
|
|
268 |
function or method definition that does not correspond to this
|
| 70405 |
lawrence |
269 |
form, it just returns its argument. \code{isRematched} returns a
|
|
|
270 |
logical value indicating whether rematching was used when
|
|
|
271 |
constructing a given method.
|
| 23443 |
hornik |
272 |
}
|
|
|
273 |
|
|
|
274 |
\item{\code{asMethodDefinition}:}{
|
| 44751 |
maechler |
275 |
Turn a function definition into an object of class
|
|
|
276 |
\code{\linkS4class{MethodDefinition}}, corresponding to the
|
| 20092 |
jmc |
277 |
given \code{signature} (by default generates a default method
|
| 23493 |
jmc |
278 |
with empty signature). The definition is sealed according to
|
|
|
279 |
the \code{sealed} argument.
|
| 23443 |
hornik |
280 |
}
|
|
|
281 |
|
| 23907 |
jmc |
282 |
\item{\code{addNextMethod}:}{
|
| 44805 |
jmc |
283 |
A generic function that finds the next method for
|
|
|
284 |
the signature of the method definition \code{method} and caches
|
|
|
285 |
that method in the method definition (promoting the class to
|
| 74363 |
maechler |
286 |
\code{"MethodWithNext"}). Note that argument \code{mlist} is
|
| 44805 |
jmc |
287 |
obsolete and not used.
|
| 23443 |
hornik |
288 |
}
|
| 26530 |
maechler |
289 |
|
|
|
290 |
\item{\code{makeGeneric}:}{
|
| 25902 |
jmc |
291 |
Makes a generic function object corresponding to the given
|
|
|
292 |
function name, optional definition and optional default method.
|
| 44751 |
maechler |
293 |
Other arguments supply optional elements for the slots of class
|
|
|
294 |
\code{\linkS4class{genericFunction}}.
|
| 25902 |
jmc |
295 |
}
|
|
|
296 |
|
| 26530 |
maechler |
297 |
\item{\code{makeStandardGeneric}:}{
|
| 25902 |
jmc |
298 |
a utility function that makes a valid function calling
|
|
|
299 |
\code{standardGeneric} for name \code{f}. Works (more or less)
|
|
|
300 |
even if the actual definition, \code{fdef}, is not a proper
|
|
|
301 |
function, that is, it's a primitive or internal.
|
|
|
302 |
}
|
| 26530 |
maechler |
303 |
|
| 25902 |
jmc |
304 |
\item{\code{conformMethod}:}{
|
|
|
305 |
If the formal arguments, \code{mnames}, are not identical to the
|
|
|
306 |
formal arguments to the function, \code{fnames},
|
|
|
307 |
\code{conformMethod} determines whether the signature and the two
|
|
|
308 |
sets of arguments conform, and returns the signature, possibly
|
|
|
309 |
extended. The function name, \code{f} is supplied for error
|
| 42839 |
maechler |
310 |
messages. The generic function, \code{fdef}, supplies the
|
|
|
311 |
generic signature for matching purposes.
|
| 25902 |
jmc |
312 |
|
| 50687 |
jmc |
313 |
The method assignment conforms if method and generic function have
|
| 25902 |
jmc |
314 |
identical formal argument lists. It can also conform if the
|
|
|
315 |
method omits some of the formal arguments of the function but: (1)
|
|
|
316 |
the non-omitted arguments are a subset of the function arguments,
|
|
|
317 |
appearing in the same order; (2) there are no arguments to the
|
| 50687 |
jmc |
318 |
method that are not arguments to the function; and (3) the omitted
|
| 25902 |
jmc |
319 |
formal arguments do not appear as explicit classes in the
|
| 50687 |
jmc |
320 |
signature. A future extension hopes to test also that the
|
| 61433 |
ripley |
321 |
omitted arguments are not assumed by being used as locally assigned
|
| 44864 |
jmc |
322 |
names or function names in the body of the method.
|
| 25902 |
jmc |
323 |
}
|
|
|
324 |
|
| 26530 |
maechler |
325 |
\item{\code{defaultDumpName}:}{
|
| 25902 |
jmc |
326 |
the default name to be used for dumping a method.
|
|
|
327 |
}
|
| 26530 |
maechler |
328 |
|
| 25902 |
jmc |
329 |
|
| 26530 |
maechler |
330 |
\item{\code{doPrimitiveMethod}:}{
|
| 25902 |
jmc |
331 |
do a primitive call to builtin function \code{name} the definition
|
|
|
332 |
and call provided, and carried out in the environment \code{ev}.
|
|
|
333 |
|
|
|
334 |
A call to \code{doPrimitiveMethod} is used when the actual method
|
|
|
335 |
is a .Primitive. (Because primitives don't behave correctly as
|
|
|
336 |
ordinary functions, not having either formal arguments nor a
|
|
|
337 |
function body).
|
|
|
338 |
}
|
|
|
339 |
|
| 28314 |
hornik |
340 |
}
|
| 19064 |
jmc |
341 |
}
|
| 33631 |
maechler |
342 |
\seealso{\code{\link{setGeneric}}, \code{\link{setClass}},
|
|
|
343 |
\code{\link{showMethods}}.
|
|
|
344 |
}
|
|
|
345 |
\examples{
|
|
|
346 |
getGroup("exp")
|
|
|
347 |
getGroup("==", recursive = TRUE)
|
|
|
348 |
|
|
|
349 |
getGroupMembers("Arith")
|
|
|
350 |
getGroupMembers("Math")
|
| 39651 |
maechler |
351 |
getGroupMembers("Ops") # -> its sub groups
|
| 33631 |
maechler |
352 |
}
|
| 16471 |
jmc |
353 |
\keyword{internal}
|