Rev 35751 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
\name{getMethod}\alias{getMethod}\alias{findMethod}\alias{existsMethod}\alias{getMethods}\alias{selectMethod}\alias{hasMethod}\alias{MethodsListSelect}\title{ Get or Test for the Definition of a Method }\description{The functions \code{getMethod} and \code{selectMethod} get thedefinition of a particular method; the functions \code{existsMethod}and \code{hasMethod} test for the existence of a method. In bothcases the first function only gets direct definitions and the seconduses inheritance.The function \code{findMethod} returns the package(s) in the searchlist (or in the packages specified by the \code{where} argument) thatcontain a method for this function and signature.The other functions are support functions: see the details below.}\usage{getMethod(f, signature=character(), where, optional=FALSE, mlist)findMethod(f, signature, where)getMethods(f, where)existsMethod(f, signature = character(), where)hasMethod(f, signature=character(), where)selectMethod(f, signature, optional = FALSE, useInherited = TRUE,mlist = (if (is.null(fdef)) NULL elsegetMethodsForDispatch(f, fdef)),fdef = getGeneric(f, !optional))MethodsListSelect(f, env, mlist, fEnv, finalDefault, evalArgs,useInherited, fdef, resetAllowed)}\arguments{\item{f}{The character-string name of the generic function.}\item{signature}{the signature of classes to match to the argumentsof \code{f}. See the details below.For \code{selectMethod}, the signature can optionally be anenvironment with classes assigned to the names of the correspondingarguments. Note: the names correspond to the names of the classes, \emph{not}to the objects supplied in a call to the generic function. (Youare not likely to find this approach convenient, but it is usedinternally and is marginally more efficient.)}\item{where}{The position or environment in which to look for themethod(s): by default, anywhere in the current search list.}\item{optional}{ If the selection does not produce a unique result,an error is generated, unless this argument is \code{TRUE}. In thatcase, the value returned is either a \code{MethodsList} object, ifmore than one method matches this signature, or \code{NULL} if nomethod matches.}\item{mlist}{Optionally, the list of methods in which to search. Bydefault, the function finds the methods for the correspondinggeneric function. To restrict the search to a particular packageor environment, e.g., supply this argument as\code{getMethodsMetaData(f,where)}. For \code{selectMethod}, seethe discussion of argument \code{fdef}.}\item{fdef}{In \code{selectMethod}, the \code{MethodsList} objectand/or the generic function object can be explicitly supplied.(Unlikely to be used, except in the recursive call that findsmatches to more than one argument.)}\item{env}{The environment in which argument evaluations are done in\code{MethodsListSelect}. Currently must be supplied, but shouldusually be \code{sys.frame(sys.parent())} when calling the functionexplicitly for debugging purposes.}\item{fEnv, finalDefault, evalArgs, useInherited, resetAllowed}{ Internal-usearguments for the function's environment, the method to use asthe overall default, whether to evaluate arguments, whicharguments should use inheritance, and whether the cached methodsare allowed to be reset.}}\details{The \code{signature} argument specifies classes, in an extendedsense, corresponding to formal arguments of the generic function.As supplied, the argument may be a vector of strings identifyingclasses, and may be named or not. Names, if supplied, match thenames of those formal arguments included in the signature of thegeneric. That signature is normally all the arguments except\dots. However, generic functions can be specified with only asubset of the arguments permitted, or with the signature takingthe arguments in a different order.It's a good idea to name the arguments in the signature to avoidconfusion, if you're dealing with a generic that does somethingspecial with its signature. In any case, the elements of thesignature are matched to the formal signature by the same rulesused in matching arguments in function calls (see\code{\link{match.call}}).The strings in the signature may be class names, \code{"missing"}or \code{"ANY"}. See \link{Methods} for the meaning of these inmethod selection. Arguments not supplied in the signatureimplicitly correspond to class \code{"ANY"}; in particular, givingan empty signature means to look for the default method.A call to \code{getMethod} returns the method for a particularfunction and signature. As with other \code{get} functions,argument \code{where} controls where the function looks (by defaultanywhere in the search list) and argument \code{optional} controlswhether the function returns \code{NULL} or generates an error ifthe method is not found. The search for the method makes no use ofinheritance.The function \code{selectMethod} also looks for a method given thefunction and signature, but makes full use of the method dispatchmechanism; i.e., inherited methods and group generics are taken intoaccount just as they would be in dispatching a method for thecorresponding signature, with the one exception that conditionalinheritance is not used. Like \code{getMethod}, \code{selectMethod}returns \code{NULL} or generates an error ifthe method is not found, depending on the argument \code{optional}.The functions \code{existsMethod} and \code{hasMethod} return\code{TRUE} or \code{FALSE} according to whether a method is found,the first corresponding to \code{getMethod} (no inheritance) and thesecond to \code{selectMethod}.The function \code{getMethods} returns all the methods for aparticular generic (in the form of a generic function with themethods information in its environment). The function is calledfrom the evaluator to merge method information, and is not intendedto be called directly. Note that it gets \emph{all} the visiblemethods for the specified functions. If you want only the methodsdefined explicitly in a particular environment, use the function\code{\link{getMethodsMetaData}} instead.The function \code{MethodsListSelect} performs a full search(including all inheritance and group generic information: see the\link{Methods} documentation page for details on how this works).The call returns a possibly revised methods list object,incorporating any method found as part of the \code{allMethods}slot.Normally you won't call \code{MethodsListSelect} directly, but it ispossible to use it for debugging purposes (only for distinctlyadvanced users!).Note that the statement that \code{MethodsListSelect} corresponds to theselection done by the evaluator is a fact, not an assertion, in thesense that the evaluator code constructs and executes a call to\code{MethodsListSelect} when it does not already have a cached methodfor this generic function and signature. (The value returned isstored by the evaluator so that the search is not required nexttime.)}\value{The call to \code{selectMethod} or \code{getMethod} returns a\code{\link{MethodDefinition-class}} object, the selected method, ifa unique selection exists.(This class extends \code{function}, so you can use the resultdirectly as a function if that is what you want.)Otherwise an error is thrown if \code{optional} is \code{FALSE}. If\code{optional} is \code{TRUE}, the value returned is \code{NULL} ifno method matched, or a \code{MethodsList} object if multiplemethods matched.The call to \code{getMethods} returns the \code{MethodsList} objectcontaining all the methods requested. If there are none,\code{NULL} is returned: \code{getMethods} does not generate anerror in this case.}\references{The R package \pkg{methods} implements, with a few exceptions, theprogramming interface for classesand methods in the book \emph{Programming with Data} (JohnM. Chambers, Springer, 1998), in particular sections 1.6, 2.7, 2.8,and chapters 7 and 8.While the programming interface for the \pkg{methods} package followsthe reference, the R software is an original implementation, sodetails in the reference that reflect the S4 implementation may appeardifferently in R. Also, there are extensions to the programminginterface developed more recently than the reference. For adiscussion of details and ongoing development, see the web page\url{http://developer.r-project.org/methodsPackage.html} and thepointers from that page.}\seealso{\code{\link{GenericFunctions}}}\examples{setGeneric("testFun", function(x)standardGeneric("testFun"))setMethod("testFun", "numeric", function(x)x+1)hasMethod("testFun", "numeric")\dontrun{[1] TRUE}hasMethod("testFun", "integer") #inherited\dontrun{[1] TRUE}existsMethod("testFun", "integer")\dontrun{[1] FALSE}hasMethod("testFun") # default method\dontrun{[1] FALSE}hasMethod("testFun", "ANY")\dontrun{[1] FALSE}\dontshow{stopifnot(isGeneric("testFun"),hasMethod("testFun", "numeric"),hasMethod("testFun", "integer"),!existsMethod("testFun", "integer"),!hasMethod("testFun"),!hasMethod("testFun", "ANY") )removeGeneric("testFun")}}\keyword{programming}\keyword{classes}\keyword{methods}