The R Project SVN R

Rev

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

Rev Author Line No. Line
15357 jmc 1
\name{getMethod}
2
\alias{getMethod}
17454 jmc 3
\alias{findMethod}
20092 jmc 4
\alias{existsMethod}
15357 jmc 5
\alias{getMethods}
6
\alias{selectMethod}
19473 jmc 7
\alias{hasMethod}
15825 jmc 8
\alias{MethodsListSelect}
20092 jmc 9
\title{ Get or Test for the Definition of a Method }
15357 jmc 10
\description{
20092 jmc 11
  The functions \code{getMethod} and \code{selectMethod} get the
12
  definition of a particular method; the functions \code{existsMethod}
13
  and \code{hasMethod} test for the existence of a method.  In both
14
  cases the first function only gets direct definitions and the second
15
  uses inheritance.
17454 jmc 16
  The function \code{findMethod} returns the package(s) in the search
17
  list (or in the packages specified by the \code{where} argument) that
18
  contain a method for this function and signature.
20092 jmc 19
 
20
  The other functions are support functions: see the details below.
15357 jmc 21
}
22
\usage{
26093 jmc 23
getMethod(f, signature=character(), where, optional=FALSE, mlist)
15357 jmc 24
 
17454 jmc 25
findMethod(f, signature, where)
26
 
26093 jmc 27
getMethods(f, where)
15825 jmc 28
 
19473 jmc 29
existsMethod(f, signature = character(), where)
30
 
26477 jmc 31
hasMethod(f, signature=character(), where)
19473 jmc 32
 
26530 maechler 33
selectMethod(f, signature, optional = FALSE, useInherited = TRUE,
34
             mlist = (if (is.null(fdef)) NULL else getMethods(fdef)),
35
             fdef = getGeneric(f, !optional))
19473 jmc 36
 
20092 jmc 37
MethodsListSelect(f, env, mlist, fEnv, finalDefault, evalArgs,
24661 ripley 38
                  useInherited, fdef, resetAllowed)
19473 jmc 39
 
15357 jmc 40
}
41
\arguments{
16559 jmc 42
  \item{f}{ The character-string name of the generic function.
43
 
23839 jmc 44
    }
19029 hornik 45
  \item{signature}{ The signature of classes to match to the arguments
23839 jmc 46
    of \code{f}.  See the details below.
18558 jmc 47
 
19029 hornik 48
    For \code{selectMethod}, the signature can optionally be an
49
    environment with classes assigned to the names of the corresponding
19473 jmc 50
    arguments.  Note:  the names correspond to the names of the classes, \emph{not}
23839 jmc 51
    to the objects supplied in a call to the generic function.  (You
52
    are not likely to find this approach convenient, but it is used
53
    internally and is marginally more efficient.)
54
  }
25902 jmc 55
  \item{where}{ The position or environment in which to look for the method(s):  by default,
19087 jmc 56
      anywhere in
57
    the current search list.}
15357 jmc 58
  \item{optional}{ If the selection does not produce a unique result,
19029 hornik 59
    an error is generated, unless this argument is \code{TRUE}.  In that
60
    case, the value returned is either a \code{MethodsList} object, if
61
    more than one method matches this signature, or \code{NULL} if no
62
    method matches.}
26093 jmc 63
  \item{mlist}{Optionally, the list of methods in which to search.  By
64
    default, the function finds the methods for the corresponding
65
    generic function.  To restrict the search to a particular package
66
    or environment, e.g., supply this argument as
26240 jmc 67
    \code{getMethodsMetaData(f,where)}.  For \code{selectMethod}, see
68
    the discussion of argument \code{fdef}.
26093 jmc 69
    }
15357 jmc 70
 
26240 jmc 71
  \item{fdef}{In \code{selectMethod}, the \code{MethodsList} object
20092 jmc 72
    and/or the generic function object can be explicitly supplied.  (Unlikely to be used, except in the
19029 hornik 73
    recursive call that finds matches to more than one argument.)}
15825 jmc 74
 
16802 jmc 75
  \item{env}{The environment in which argument evaluations are done in
15825 jmc 76
    \code{MethodsListSelect}.  Currently must be supplied, but should
19029 hornik 77
    usually be \code{sys.frame(sys.parent())} when calling the function
78
    explicitly for debugging purposes.}
24661 ripley 79
  \item{fEnv, finalDefault, evalArgs, useInherited, resetAllowed}{ Internal-use
19029 hornik 80
    arguments for the function's environment, the method to use as
24661 ripley 81
    the overall default, whether to evaluate arguments, which
82
    arguments should use inheritance, and whether the cached methods
83
    are allowed to be reset.}
15357 jmc 84
}
85
\details{
23839 jmc 86
  The \code{signature} argument specifies classes, in an extended
87
    sense, corresponding to formal arguments of the generic function.
88
    As supplied, the argument may be a vector of strings identifying
89
    classes, and may be named or not.  Names, if supplied, match the
90
    names of those formal arguments included in the signature of the
91
    generic.  That signature is normally all the arguments except
92
    \dots.  However, generic functions can be specified with only a
93
    subset of the arguments permitted, or with the signature taking
94
    the arguments in a different order.
95
 
96
    It's a good idea to name the arguments in the signature to avoid
97
    confusion, if you're dealing with a generic that does something
98
    special with its signature.   In any case, the elements of the
99
    signature are matched to the formal signature by the same rules
100
    used in matching arguments in function calls (see
101
    \code{\link{match.call}}).
102
 
103
    The strings in the signature may be class names, \code{"missing"}
104
    or \code{"ANY"}.  See \link{Methods} for the meaning of these in
105
    method selection.  Arguments not supplied in the signature
106
    implicitly correspond to class \code{"ANY"}; in particular, giving
107
    an empty signature means to look for the default method.
108
 
16802 jmc 109
  A call to \code{getMethod} returns the method for a particular
110
  function and signature.  As with other \code{get} functions,
111
  argument \code{where} controls where the function looks (by default
112
  anywhere in the search list) and argument \code{optional} controls
113
  whether the function returns \code{NULL} or generates an error if
114
  the method is not found.  The search for the method makes no use of
115
  inheritance.
15357 jmc 116
 
19473 jmc 117
  The function \code{selectMethod} also looks for a method given the
118
  function and signature, but makes full use of the method dispatch
119
  mechanism; i.e., inherited methods and group generics are taken into
120
  account just as they would be in dispatching a method for the
121
  corresponding signature, with the one exception that conditional
122
  inheritance is not used.  Like \code{getMethod}, \code{selectMethod}
123
  returns \code{NULL} or generates an error if
124
  the method is not found, depending on the argument \code{optional}.
125
 
126
  The functions \code{existsMethod} and \code{hasMethod} return
127
  \code{TRUE} or \code{FALSE} according to whether a method is found,
128
  the first corresponding to \code{getMethod} (no inheritance) and the
129
  second to \code{selectMethod}.
130
 
16802 jmc 131
  The function \code{getMethods} returns all the methods for a
132
  particular generic (in the form of a generic function with the
133
  methods information in its environment).  The function is called
134
  from the evaluator to merge method information, and is not intended
25902 jmc 135
  to be called directly.  Note that it gets \emph{all} the visible
136
  methods for the specified functions.  If you want only the methods
137
  defined explicitly in a particular environment, use the function
138
  \code{\link{getMethodsMetaData}} instead.
15357 jmc 139
 
16802 jmc 140
  The function \code{MethodsListSelect} performs a full search
141
  (including all inheritance and group generic information: see the
142
  \link{Methods} documentation page for details on how this works).
143
  The call returns a possibly revised methods list object,
144
  incorporating any method found as part of the \code{allMethods}
145
  slot.
15357 jmc 146
 
15825 jmc 147
  Normally you won't call \code{MethodsListSelect} directly, but it is
148
  possible to use it for debugging purposes (only for distinctly
149
  advanced users!).
150
 
151
  Note that the statement that \code{MethodsListSelect} corresponds to the
15357 jmc 152
  selection done by the evaluator is a fact, not an assertion, in the
153
  sense that the evaluator code constructs and executes a call to
16802 jmc 154
  \code{MethodsListSelect} when it does not already have a cached method
15357 jmc 155
  for this generic function and signature.  (The value returned is
16802 jmc 156
  stored by the evaluator so that the search is not required next
157
  time.)
158
 
19473 jmc 159
 
15357 jmc 160
}
161
\value{
162
  The call to \code{selectMethod} or \code{getMethod} returns a
20092 jmc 163
  \code{\link{MethodDefinition-class}} object, the selected method, if
164
  a unique selection exists.
165
  (This class extends \code{function}, so you can use the result
166
  directly as a function if that is what you want.)
15357 jmc 167
  Otherwise an error is thrown if \code{optional} is \code{FALSE}.  If
168
  \code{optional} is \code{TRUE}, the value returned is \code{NULL} if
169
  no method matched, or a \code{MethodsList} object if multiple
170
  methods matched.
171
 
172
  The call to \code{getMethods} returns the \code{MethodsList} object
173
  containing all the methods requested.  If there are none,
174
  \code{NULL} is returned: \code{getMethods} does not generate an
175
  error in this case.
176
}
177
\references{
25118 hornik 178
  The R package \pkg{methods} implements, with a few exceptions, the
21345 jmc 179
  programming interface for classes
180
  and methods in the book \emph{Programming with Data} (John
181
  M. Chambers, Springer, 1998), in particular sections 1.6, 2.7, 2.8,
182
  and chapters 7 and 8.
15357 jmc 183
 
25118 hornik 184
  While the programming interface for the \pkg{methods} package follows
185
  the reference, the R software is an original implementation, so
186
  details in the reference that reflect the S4 implementation may appear
21345 jmc 187
  differently in R.  Also, there are extensions to the programming
188
  interface developed more recently than the reference.  For a
26530 maechler 189
  discussion of details and ongoing development, see the web page
21345 jmc 190
  \url{http://developer.r-project.org/methodsPackage.html} and the
191
  pointers from that page.
15357 jmc 192
}
26477 jmc 193
\examples{
194
setGeneric("testFun", function(x)standardGeneric("testFun"))
195
setMethod("testFun", "numeric", function(x)x+1)
196
hasMethod("testFun", "numeric")
197
\dontrun{[1] TRUE}
198
hasMethod("testFun", "integer") #inherited
199
\dontrun{[1] TRUE}
200
existsMethod("testFun", "integer")
201
\dontrun{[1] FALSE}
202
hasMethod("testFun") # default method
203
\dontrun{[1] FALSE}
204
hasMethod("testFun", "ANY")
205
\dontrun{[1] FALSE}
206
\dontshow{
207
stopifnot(isGeneric("testFun"))
208
stopifnot(hasMethod("testFun", "numeric"))
209
stopifnot(hasMethod("testFun", "integer"))
210
stopifnot(!existsMethod("testFun", "integer"))
211
stopifnot(!hasMethod("testFun"))
212
stopifnot(!hasMethod("testFun", "ANY"))
213
removeGeneric("testFun")
214
}
215
}
15357 jmc 216
\keyword{programming}
217
\keyword{classes}
218
\keyword{methods}