Rev 88585 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
% File src/library/methods/man/getMethod.Rd% Part of the R package, https://www.R-project.org% Copyright 1995-2016 R Core Team% Distributed under GPL 2 or later\name{getMethod}\title{Get or Test for the Definition of a Method}\alias{getMethod}\alias{findMethod}\alias{existsMethod}\alias{selectMethod}\alias{hasMethod}\description{The function \code{selectMethod()} returns the method thatwould be selected for a call to function \code{f} if the arguments hadclasses as specified by \code{signature}. Failing to find a methodis an error, unless argument \code{optional = TRUE}, in which case\code{NULL} is returned.The function \code{findMethod()} returns a list ofenvironments that contain a method for the specified function and signature; bydefault, these are a subset of the packages in the current searchlist. See section \dQuote{Using \code{findMethod()}} for details.The function \code{getMethod()} returns the method corresponding to thefunction and signature supplied similarly to \code{selectMethod}, butwithout using inheritance or group generics.The functions \code{hasMethod()} and\code{existsMethod()} test whether \code{selectMethod()} or\code{getMethod()}, respectively, finds a matching method.}\usage{selectMethod(f, signature, optional = FALSE, useInherited =,mlist = , fdef = , verbose = , doCache = )findMethod(f, signature, where)getMethod(f, signature = character(), where, optional = FALSE,mlist, fdef)existsMethod(f, signature = character(), where)hasMethod(f, signature = character(), where)}\arguments{\item{f}{a generic function or the character-string name of one.}\item{signature}{the signature of classes to match to the argumentsof \code{f}. See the details below.}\item{where}{the environment in which to look for themethod(s). By default, if the call comes from the command line, the table of methods defined in the genericfunction itself is used, except for \code{findMethod} (see thesection below).}\item{optional}{if the selection in \code{selectMethod} does not finda valid method an error is generated, unless \code{optional} is\code{TRUE}, in which case the value returned is \code{NULL}.}\item{mlist, fdef, useInherited, verbose, doCache}{optional argumentsto \code{getMethod} and \code{selectMethod} for internal use. Avoidthese: some will work as expected and others will not, and none ofthem is required for normal use of the functions. But see thesection \dQuote{Methods for \code{as()}} for nonstandard inheritance.}}\section{Using \code{findMethod()}}{As its name suggests, this function is intended to behave like\code{\link{find}}, which produces a list of the packages on thecurrent search list which have, and have exported, the object named.That's what \code{findMethod} does also, by default. The\dQuote{exported} part in this case means that the package's namespacehas an \code{exportMethods} directive for this generic function.An important distinction is that the absence of such a directive doesnot prevent methods from the package from being called once thepackage is loaded. Otherwise, the code in the package could not useun-exported methods.So, if your question is whether loading package \code{thisPkg} will define amethod for this function and signature, you need to ask that questionabout the namespace of the package:\code{findMethod(f, signature, where = asNamespace("thisPkg"))}If the package did not export the method, attaching it and calling\code{findMethod} with no \code{where} argument will not find themethod.Notice also that the length of the signature must be what thecorresponding package used. If \code{thisPkg} had only methods forone argument, only length-1 signatures will match (no trailing\code{"ANY"}), even if another currently loaded package had signatureswith more arguments.}\section{Methods for \code{as()}}{The function \code{\link{setAs}} allows packages to define methods forcoercing one class of objects to another class. This works internallyby defining methods for the generic function \code{\link{coerce}(from,to)},which can not be called directly.The \R evaluator selectsmethods for this purpose using a different form of inheritance. Whilemethods can be inherited for the object being coerced, they cannotinherit for the target class, since the result would not be a validobject from that class.If you want toexamine the selection procedure, you must supply the optional argument\code{useInherited = c(TRUE, FALSE)} to \code{selectMethod}.}\details{The \code{signature} argument specifies classes, corresponding toformal arguments of the generic function; to be precise, to the\code{signature} slot of the generic function object. The argumentmay be a vector of strings identifying classes, and may be named ornot. Names, if supplied, match the names of those formal argumentsincluded in the signature of the generic. That signature is normallyall the arguments except \dots. However, generic functions can bespecified with only a subset of the arguments permitted, or with thesignature taking the 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 rules usedin 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_Details} for the meaning of these in methodselection. Arguments not supplied in the signature implicitlycorrespond to class \code{"ANY"}; in particular, giving an emptysignature means to look for the default method.A call to \code{getMethod} returns the method for a particularfunction and signature. 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}.Both \code{selectMethod} and \code{getMethod} will normally use thecurrent version of the generic function in the R session, which has atable of the methods obtained from all the packages loaded in thesession. Optional arguments can cause a search for the generic function from aspecified environment, but this is rarely a useful idea. In contrast,\code{findMethod} has a different default and the optional\code{where=} argument may be needed. See the section \dQuote{Using\code{findMethod()}}.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}.}\value{The call to \code{selectMethod} or \code{getMethod} returns the selected method, ifone is found.(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} and \code{NULL} is returned if\code{optional} is \code{TRUE}.The returned method object is a\code{\linkS4class{MethodDefinition}} object, \emph{except} that the default method for a primitive function is required to be the primitive itself.Note therefore that the only reliable test that the search failed is\code{is.null()}.The returned value of \code{findMethod} is a list ofenvironments in which a corresponding method was found; that is, atable of methods including the one specified.}\references{\bibshow{R:Chambers:2016}(Chapters 9 and 10.)\bibshow{R:Chambers:2008}(Section 10.6 for some details of method selection.)}\seealso{\code{\link{Methods_Details}} for the details of method selection;\code{\link{GenericFunctions}} for other functions manipulatingmethods and generic function objects;\code{\linkS4class{MethodDefinition}} for the class that representsmethod definitions.}\examples{testFun <- function(x)xsetGeneric("testFun")setMethod("testFun", "numeric", function(x)x+1)hasMethod("testFun", "numeric") # TRUEhasMethod("testFun", "integer") #TRUE, inheritedexistsMethod("testFun", "integer") #FALSEhasMethod("testFun") # TRUE, default methodhasMethod("testFun", "ANY")\dontshow{## Verify the examplestopifnot(isGeneric("testFun"),hasMethod("testFun", "numeric"),hasMethod("testFun", "integer"),!existsMethod("testFun", "integer"),hasMethod("testFun"),hasMethod("testFun", "ANY") )removeGeneric("testFun")}}\keyword{programming}\keyword{classes}\keyword{methods}