The R Project SVN R

Rev

Rev 58700 | Rev 70994 | 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/getMethod.Rd
2
% Part of the R package, http://www.R-project.org
59039 ripley 3
% Copyright 1995-2012 R Core Team
42333 ripley 4
% Distributed under GPL 2 or later
5
 
15357 jmc 6
\name{getMethod}
56186 murdoch 7
\alias{getMethod}
17454 jmc 8
\alias{findMethod}
20092 jmc 9
\alias{existsMethod}
15357 jmc 10
\alias{selectMethod}
19473 jmc 11
\alias{hasMethod}
20092 jmc 12
\title{ Get or Test for the Definition of a Method }
15357 jmc 13
\description{
45824 jmc 14
  Functions to look for a method corresponding to a given generic function and signature.
58700 maechler 15
  The functions \code{getMethod} and \code{selectMethod} return the method; the functions
16
  \code{existsMethod} and \code{hasMethod} test for its existence.  In both
20092 jmc 17
  cases the first function only gets direct definitions and the second
58700 maechler 18
  uses inheritance.  In all cases, the search is in the generic function itself or in
19
  the package/environment specified by argument \code{where}.
45824 jmc 20
 
17454 jmc 21
  The function \code{findMethod} returns the package(s) in the search
22
  list (or in the packages specified by the \code{where} argument) that
23
  contain a method for this function and signature.
15357 jmc 24
}
25
\usage{
44243 ripley 26
getMethod(f, signature=character(), where, optional = FALSE,
27
          mlist, fdef)
15357 jmc 28
 
45824 jmc 29
existsMethod(f, signature = character(), where)
30
 
17454 jmc 31
findMethod(f, signature, where)
32
 
45824 jmc 33
selectMethod(f, signature, optional = FALSE, useInherited =,
58700 maechler 34
             mlist = , fdef = , verbose = , doCache = )
19473 jmc 35
 
26477 jmc 36
hasMethod(f, signature=character(), where)
19473 jmc 37
 
15357 jmc 38
}
39
\arguments{
45824 jmc 40
  \item{f}{A  generic function or the character-string name of one.}
33634 maechler 41
  \item{signature}{the signature of classes to match to the arguments
23839 jmc 42
    of \code{f}.  See the details below.
18558 jmc 43
 
23839 jmc 44
  }
33634 maechler 45
  \item{where}{The position or environment in which to look for the
45824 jmc 46
    method(s):  by default, the table of methods defined in the generic function itself is used.}
47
 
58487 maechler 48
  \item{optional}{If the selection in \code{selectMethod} does not find a valid method
19029 hornik 49
    an error is generated, unless this argument is \code{TRUE}.  In that
58487 maechler 50
    case, the value returned is \code{NULL} if no method matches.}
58700 maechler 51
  \item{mlist, fdef, useInherited, verbose, doCache}{Optional
51579 jmc 52
      arguments  to  \code{getMethod} and \code{selectMethod} for
53
      internal use.  Avoid
54
      these: some will work as expected and others will not, and none of them is required for normal use of the functions.
26093 jmc 55
    }
45824 jmc 56
 
15357 jmc 57
}
58
\details{
45824 jmc 59
  The \code{signature} argument specifies classes, corresponding to formal arguments of the generic function; to be precise, to the \code{signature} slot of the generic function object.
60
    The argument may be a vector of strings identifying
23839 jmc 61
    classes, and may be named or not.  Names, if supplied, match the
62
    names of those formal arguments included in the signature of the
63
    generic.  That signature is normally all the arguments except
64
    \dots.  However, generic functions can be specified with only a
65
    subset of the arguments permitted, or with the signature taking
66
    the arguments in a different order.
67
 
68
    It's a good idea to name the arguments in the signature to avoid
69
    confusion, if you're dealing with a generic that does something
70
    special with its signature.   In any case, the elements of the
71
    signature are matched to the formal signature by the same rules
72
    used in matching arguments in function calls (see
73
    \code{\link{match.call}}).
74
 
75
    The strings in the signature may be class names, \code{"missing"}
76
    or \code{"ANY"}.  See \link{Methods} for the meaning of these in
77
    method selection.  Arguments not supplied in the signature
78
    implicitly correspond to class \code{"ANY"}; in particular, giving
79
    an empty signature means to look for the default method.
80
 
16802 jmc 81
  A call to \code{getMethod} returns the method for a particular
82
  function and signature.  As with other \code{get} functions,
83
  argument \code{where} controls where the function looks (by default
84
  anywhere in the search list) and argument \code{optional} controls
85
  whether the function returns \code{NULL} or generates an error if
86
  the method is not found.  The search for the method makes no use of
87
  inheritance.
15357 jmc 88
 
19473 jmc 89
  The function \code{selectMethod} also looks for a method given the
90
  function and signature, but makes full use of the method dispatch
91
  mechanism; i.e., inherited methods and group generics are taken into
92
  account just as they would be in dispatching a method for the
93
  corresponding signature, with the one exception that conditional
94
  inheritance is not used.  Like \code{getMethod}, \code{selectMethod}
95
  returns \code{NULL} or generates an error if
96
  the method is not found, depending on the argument \code{optional}.
97
 
98
  The functions \code{existsMethod} and \code{hasMethod} return
99
  \code{TRUE} or \code{FALSE} according to whether a method is found,
100
  the first corresponding to \code{getMethod} (no inheritance) and the
101
  second to \code{selectMethod}.
102
 
15357 jmc 103
}
104
\value{
45824 jmc 105
  The call to \code{selectMethod} or \code{getMethod} returns  the selected method, if
106
 one is found.
20092 jmc 107
  (This class extends \code{function}, so you can use the result
108
  directly as a function if that is what you want.)
45824 jmc 109
  Otherwise an error is thrown if \code{optional} is \code{FALSE}  and  \code{NULL} is returned if
110
  \code{optional} is \code{TRUE}.
15357 jmc 111
 
45824 jmc 112
The returned method object is a
113
  \code{\linkS4class{MethodDefinition}} object, \emph{except} that the default method for a primitive function is required to be the primitive itself.
114
Note therefore that the only reliable test that the search failed is \code{is.null()}.
115
 
15357 jmc 116
}
117
\references{
45824 jmc 118
 Chambers, John M. (2008)
119
 \emph{Software for Data Analysis: Programming with R}
120
  Springer.  (For the R version.)
15357 jmc 121
 
45824 jmc 122
 Chambers, John M. (1998)
123
 \emph{Programming with Data}
124
 Springer (For the original S4 version.)
15357 jmc 125
}
45824 jmc 126
\seealso{\code{\link{Methods}} for the details of method
127
  selection; \code{\link{GenericFunctions}} for other functions
128
  manipulating methods and generic function objects;
129
  \code{\linkS4class{MethodDefinition}} for the class that represents
130
  method definitions.}
26477 jmc 131
\examples{
132
setGeneric("testFun", function(x)standardGeneric("testFun"))
133
setMethod("testFun", "numeric", function(x)x+1)
134
hasMethod("testFun", "numeric")
135
\dontrun{[1] TRUE}
136
hasMethod("testFun", "integer") #inherited
137
\dontrun{[1] TRUE}
138
existsMethod("testFun", "integer")
139
\dontrun{[1] FALSE}
140
hasMethod("testFun") # default method
141
\dontrun{[1] FALSE}
142
hasMethod("testFun", "ANY")
143
\dontrun{[1] FALSE}
144
\dontshow{
33634 maechler 145
stopifnot(isGeneric("testFun"),
47616 ripley 146
          hasMethod("testFun", "numeric"),
147
          hasMethod("testFun", "integer"),
148
          !existsMethod("testFun", "integer"),
149
          !hasMethod("testFun"),
150
          !hasMethod("testFun", "ANY") )
26477 jmc 151
removeGeneric("testFun")
152
}
153
}
15357 jmc 154
\keyword{programming}
155
\keyword{classes}
156
\keyword{methods}