The R Project SVN R

Rev

Rev 70405 | Rev 86513 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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