The R Project SVN R

Rev

Rev 56382 | Rev 65208 | 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/base/man/UseMethod.Rd
2
% Part of the R package, http://www.R-project.org
59039 ripley 3
% Copyright 1995-2010 R Core Team
42333 ripley 4
% Distributed under GPL 2 or later
5
 
24500 ripley 6
\name{UseMethod}
7
\title{Class Methods}
56186 murdoch 8
\alias{UseMethod}
24500 ripley 9
\alias{NextMethod}
10
\alias{S3Methods}
38429 ripley 11
\alias{.Method}
12
\alias{.Generic}
13
\alias{.Class}
24500 ripley 14
\description{
15
  \R possesses a simple generic function mechanism which can be used for
36334 ripley 16
  an object-oriented style of programming.  Method dispatch takes place
38429 ripley 17
  based on the class(es) of the first argument to the generic function or of
24500 ripley 18
  the object supplied as an argument to \code{UseMethod} or \code{NextMethod}.
19
}
20
\usage{
21
UseMethod(generic, object)
38429 ripley 22
 
24500 ripley 23
NextMethod(generic = NULL, object = NULL, \dots)
24
}
25
\arguments{
38148 ripley 26
  \item{generic}{a character string naming a function (and not a
27
    built-in operator).  Required for \code{UseMethod}.}
37776 ripley 28
  \item{object}{for \code{UseMethod}: an object whose class will
29
    determine the method to be dispatched.  Defaults to the first
30
    argument of the enclosing function.}
31
  \item{\dots}{further arguments to be passed to the next method.}
24500 ripley 32
}
33
\details{
42961 ripley 34
  An \R object is a data object which has a \code{class}
41079 ripley 35
  attribute (and this can be tested by \code{\link{is.object}}).
24500 ripley 36
  A class attribute is a character vector giving the names of
42961 ripley 37
  the classes from which the object \emph{inherits}.
24500 ripley 38
  If the object does not have a class attribute, it has an implicit
32148 ripley 39
  class.  Matrices and arrays have class \code{"matrix"}
40
  or\code{"array"} followed by the class of the underlying vector.
36334 ripley 41
  Most vectors have class the result of \code{\link{mode}(x)}, except
32148 ripley 42
  that integer vectors have class \code{c("integer", "numeric")} and
43
  real vectors have class \code{c("double", "numeric")}.
24500 ripley 44
 
38148 ripley 45
  When a function calling \code{UseMethod("fun")} is applied to an
46
  object with class attribute \code{c("first", "second")}, the system
47
  searches for a function called \code{fun.first} and, if it finds it,
48
  applies it to the object.  If no such function is found a function
49
  called \code{fun.second} is tried.  If no class name produces a
50
  suitable function, the function \code{fun.default} is used, if it
51
  exists, or an error results.
24500 ripley 52
 
30461 ripley 53
  Function \code{\link{methods}} can be used to find out about the
24500 ripley 54
  methods for a particular generic function or class.
55
 
51265 ripley 56
  \code{UseMethod} is a primitive function but (as from \R 2.11.0) uses
57
  standard argument matching.  It is not the only means of dispatch of
51271 ripley 58
  methods, for there are \link{internal generic} and \link{group generic}
59
  functions.  \code{UseMethod} currently dispatches on the implicit
60
  class even for arguments that are not objects, but the other means of
61
  dispatch do not.
36054 ripley 62
 
24500 ripley 63
  \code{NextMethod} invokes the next method (determined by the
39493 ripley 64
  class vector, either of the object supplied to the generic, or of
37776 ripley 65
  the first argument to the function containing \code{NextMethod} if a
66
  method was invoked directly).  Normally \code{NextMethod} is used with
67
  only one argument, \code{generic}, but if further arguments are
38429 ripley 68
  supplied these modify the call to the next method.
24500 ripley 69
 
70
  \code{NextMethod} should not be called except in methods called by
39493 ripley 71
  \code{UseMethod} or from internal generics (see
72
  \link{InternalGenerics}).  In particular it will not work inside
73
  anonymous calling functions (e.g. \code{get("print.ts")(AirPassengers)}).
24500 ripley 74
 
56382 murdoch 75
  Namespaces can register methods for generic functions.  To support
25669 luke 76
  this, \code{UseMethod} and \code{NextMethod} search for methods in
77
  two places: first in the environment in which the generic function
78
  is called, and then in the registration data base for the
56382 murdoch 79
  environment in which the generic is defined (typically a namespace).
38148 ripley 80
  So methods for a generic function need to be available in the
25669 luke 81
  environment of the call to the generic, or they must be registered.
38148 ripley 82
  (It does not matter whether they are visible in the environment in
38158 ripley 83
  which the generic is defined.)
24500 ripley 84
}
38429 ripley 85
\section{Technical Details}{
86
  Now for some obscure details that need to appear somewhere.  These
87
  comments will be slightly different than those in Chambers(1992).
88
  (See also the draft \sQuote{R Language Definition}.)
42961 ripley 89
  \code{UseMethod} creates a new function call with
38429 ripley 90
  arguments matched as they came in to the generic.  Any local variables
91
  defined before the call to \code{UseMethod} are retained (unlike S).  Any
92
  statements after the call to \code{UseMethod} will not be evaluated as
93
  \code{UseMethod} does not return.  \code{UseMethod} can be called with
94
  more than two arguments: a warning will be given and additional
95
  arguments ignored.  (They are not completely ignored in S.)  If it is
96
  called with just one argument, the class of the first argument of the
97
  enclosing function is used as \code{object}: unlike S this is the first
98
  actual argument passed and not the current value of the object of that
99
  name.
100
 
101
  \code{NextMethod} works by creating a special call frame for the next
102
  method.  If no new arguments are supplied, the arguments will be the
103
  same in number, order and name as those to the current method but
104
  their values will be promises to evaluate their name in the current
105
  method and environment.  Any named arguments matched to \code{\dots}
106
  are handled specially: they either replace existing arguments of the
107
  same name or are appended to the argument list.  They are passed on as
108
  the promise that was supplied as an argument to the current
55555 ripley 109
  environment.  (S does this differently!)  If they have been evaluated
38429 ripley 110
  in the current (or a previous environment) they remain evaluated.
111
  (This is a complex area, and subject to change: see the draft
112
  \sQuote{R Language Definition}.)
113
 
39508 ripley 114
  The search for methods for \code{NextMethod} is slightly different
115
  from that for \code{UseMethod}.   Finding no \code{fun.default} is not
116
  necessarily an error, as the search continues to the generic
55555 ripley 117
  itself.  This is to pick up an \link{internal generic} like \code{[}
39508 ripley 118
  which has no separate default method, and succeeds only if the generic
119
  is a \link{primitive} function or a wrapper for a
120
  \code{\link{.Internal}} function of the same name.  (When a primitive
121
  is called as the default method, argument matching may not work as
122
  described above due to the different semantics of primitives.)
123
 
38429 ripley 124
  You will see objects such as \code{.Generic}, \code{.Method}, and
125
  \code{.Class} used in methods.  These are set in the environment
126
  within which the method is evaluated by the dispatch mechanism, which
127
  is as follows:
128
  \enumerate{
129
    \item Find the context for the calling function (the generic): this
130
    gives us the unevaluated arguments for the original call.
131
    \item Evaluate the object (usually an argument) to be used for
132
    dispatch, and find a method (possibly the default method) or throw
133
    an error.
134
    \item Create an environment for evaluating the method and insert
135
    special variables (see below) into that environment.  Also copy any
136
    variables in the environment of the generic that are not formal (or
137
    actual) arguments.
138
    \item Fix up the argument list to be the arguments of the call
139
    matched to the formals of the method.
140
  }
141
  \code{.Generic} is a length-one character vector naming the generic function.
142
 
143
  \code{.Method} is a character vector (normally of length one) naming
144
  the method function.  (For functions in the group generic
49567 ripley 145
  \code{\link[=S3groupGeneric]{Ops}} it is of length two.)
38429 ripley 146
 
147
  \code{.Class} is a character vector of classes used to find the next
148
  method.  \code{NextMethod} adds an attribute \code{"previous"} to
149
  \code{.Class} giving the \code{.Class} last used for dispatch, and
150
  shifts \code{.Class} along to that used for dispatch.
151
 
152
  \code{.GenericCallEnv} and \code{.GenericDefEnv} are the environments
153
  of the call to be generic and defining the generic respectively.  (The
154
  latter is used to find methods registered for the generic.)
155
 
156
  Note that \code{.Class} is set when the generic is called, and is
157
  unchanged if the class of the dispatching argument is changed in a
158
  method.  It is possible to change the method that \code{NextMethod}
159
  would dispatch by manipulating \code{.Class}, but \sQuote{this is not
160
    recommended unless you understand the inheritance mechanism
47262 ripley 161
    thoroughly} (Chambers & Hastie, 1992, p. 469).
38429 ripley 162
}
24500 ripley 163
\note{
164
  This scheme is called \emph{S3} (S version 3).  For new projects,
165
  it is recommended to use the more flexible and robust \emph{S4} scheme
38429 ripley 166
  provided in the \pkg{methods} package.
24500 ripley 167
}
168
\seealso{
37776 ripley 169
  The draft \sQuote{R Language Definition}.
38429 ripley 170
 
49567 ripley 171
  \code{\link{methods}}, \code{\link{class}},
41079 ripley 172
  \code{\link{getS3method}}, \code{\link{is.object}}.
24500 ripley 173
}
174
\references{
175
  Chambers, J. M. (1992)
176
  \emph{Classes and methods: object-oriented programming in S.}
177
  Appendix A of \emph{Statistical Models in S}
47262 ripley 178
  eds J. M. Chambers and T. J. Hastie, Wadsworth & Brooks/Cole.
24500 ripley 179
}
180
\keyword{methods}