The R Project SVN R

Rev

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

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