The R Project SVN R

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
15357 jmc 1
\name{Methods}
16802 jmc 2
\alias{Methods}
17000 jmc 3
\title{General Information on Methods}
15357 jmc 4
\description{
19029 hornik 5
  This documentation section covers some general topics on how methods
25118 hornik 6
  work and how the \pkg{methods} package interacts with the rest of R.  The
19029 hornik 7
  information is usually not needed to get started with methods and
8
  classes, but may be helpful for moderately ambitious projects, or when
9
  something doesn't work as expected.
15357 jmc 10
 
19029 hornik 11
  The section \bold{How Methods Work} describes the underlying
12
  mechanism; \bold{Class Inheritance and Method Selection} provides more
13
  details on how class definitions determine which methods are used.
16764 jmc 14
 
19029 hornik 15
  The section \bold{Changes with the Methods Package} outlines possible
25118 hornik 16
  effects on other computations when running with package \pkg{methods}.
16559 jmc 17
}
16764 jmc 18
\section{How Methods Work}{
19029 hornik 19
  A generic function is a function that has associated with it a
20
  collection of other functions (the methods), all of which agree in
25118 hornik 21
  formal arguments with the generic.  In R, the \dQuote{collection} is an
19029 hornik 22
  object of class \code{"\link{MethodsList}"}, which contains a named
23
  list of methods (the \code{methods} slot), and the name of one of the
24
  formal arguments to the function (the \code{argument} slot).  The
25
  names of the methods are the names of classes, and the corresponding
26
  element defines the method or methods to be used if the corresponding
27
  argument has that class.  For example, suppose a function \code{f} has
28
  formal arguments \code{x} and \code{y}.  The methods list object for
29
  that function has the object \code{as.name("x")} as its
30
  \code{argument} slot.  An element of the methods named \code{"track"}
31
  is selected if the actual argument corresponding to \code{x} is an
32
  object of class \code{"track"}.  If there is such an element, it can
33
  generally be either a function or another methods list object.
16764 jmc 34
 
19029 hornik 35
  In the first case, the function defines the method to use for any call
36
  in which \code{x} is of class \code{"track"}.  In the second case, the
37
  new methods list object defines the selection of methods depending on
38
  the remaining formal arguments, in this example, \code{y}.  The same
39
  selection process takes place, recursively, using the new methods list.
40
  Eventually, the selection returns either a function or \code{NULL},
41
  meaning that no method matched the actual arguments.
16764 jmc 42
 
19029 hornik 43
  Each method selected corresponds conceptually to a \emph{signature};
44
  that is a named list of classes, with names corresponding to some or
45
  all of the formal arguments.  In the previous example, if selecting
46
  class \code{"track"} for \code{x}, finding that the selection was
47
  another methods list and then selecting class \code{"numeric"} for
48
  \code{y} would produce a method associated with the signature
49
  \code{x = "track", y = "numeric"}.
16764 jmc 50
 
19029 hornik 51
  The actual selection is done recursively, but you can see the methods
52
  arranged by signature by calling the function
53
  \code{\link{showMethods}}, and objects with the methods arranged this
54
  way (in two different forms) are returned by the functions
55
  \code{\link{listFromMlist}} and \code{\link{linearizeMlist}}.
16764 jmc 56
 
19029 hornik 57
  In an R session, each generic function has a single methods list
58
  object defining all the currently available methods.  The session
59
  methods list object is created the first time the function is called
60
  by merging all the relevant method definitions currently visible.
61
  Whenever something happens that might change the definitions (such as
62
  attaching or detaching a package with methods for this function, or
63
  explicitly defining or removing methods), the merged methods list
64
  object is removed.  The next call to the function will recompute the
65
  merged definitions.
16764 jmc 66
 
19029 hornik 67
  When methods list are merged, they can come from two sources:
68
  \enumerate{
23443 hornik 69
    \item Methods list objects for the same function anywhere on the
70
    current search list.  These are merged so that methods in an
71
    environment earlier in the search list override methods for the same
72
    function later in the search list.  A method overrides only 
73
    another method for the same signature.  See the comments on class
74
    \code{"ANY"} in the section on \bold{Inheritance}.
75
    \item Methods list objects corresponding the group generic
76
    functions, if any, for this function.  Any generic function can be
77
    defined to belong to a group generic.  The methods for the group
78
    generic are available as methods for this function.  The group
79
    generic can itself be defined as belong to a group; as a result
80
    there is a list of group generic functions.  A method defined for a
81
    function and a particular signature overrides a method for the same
82
    signature for that function's group generic.
19029 hornik 83
  }
19087 jmc 84
  Merging is done first on all methods for a particular function, and
19029 hornik 85
  then over the generic and its group generics.
16764 jmc 86
 
19029 hornik 87
  The result is a single methods list object that contains all the
88
  methods \emph{directly} defined for this function.  As calls to the
89
  function occur, this information may be supplemented by
90
  \emph{inherited} methods, which we consider next.
16764 jmc 91
}
92
\section{Class Inheritance and Method Selection}{
93
If no method is found directly for the actual arguments in a call to a
94
generic function, an attempt is made to match the available methods to
95
the arguments by using \emph{inheritance}.
96
 
97
Each class definition potentially includes the names of one or more
23678 jmc 98
classes that the new class contains.  (These are sometimes called the
16764 jmc 99
\emph{superclasses} of the new class.)  These classes themselves may
100
extend other classes.  Putting all this information together produces
101
the full list of superclasses for this class. (You can see this list
102
for any class \code{"A"} from the expression \code{extends("A")}.)
103
In addition, any class implicitly extends class \code{"ANY"}.
23678 jmc 104
When all the superclasses are needed, as they are for dispatching
105
methods, they are ordered by how direct they are:  first, the direct
106
classes contained directly in the definition of this class, then the
107
superclasses of these classes, etc.
16764 jmc 108
 
23678 jmc 109
The S language has an additional, explicit mechanism for defining superclasses, the
110
\code{\link{setIs}} mechanism.
111
This mechanism allows a class to extend another even though they do
112
not have the same representation.  The extension is made possible by
113
defining explicit methods to \code{coerce} an object to its superclass
114
and to \code{replace} the data in the object corresponding to the
115
superclass.  The \code{\link{setIs}} mechanism will be used less often
116
and only when directly including the superclass does not make sense,
117
but once defined, the superclass acts just as directly contained
118
classes as far as method selection is concerned.
119
 
16764 jmc 120
A method will be selected by inheritance if we can find a method in
121
the methods list for a signature corresponding to any
122
combination of superclasses for each of the relevant arguments.
123
The search for such a method is performed by the function
124
\code{\link{MethodsListSelect}}, working as follows.
125
 
126
 
23678 jmc 127
  The generic, \code{f} say,  has a signature, which by default
128
  is all its formal arguments, except \dots (see
129
  \code{\link{setGeneric}}). For each of the formal arguments in that
130
  signature, in order, the class of the actual argument is matched
131
  against available methods.  A missing argument corresponds to class
132
  \code{"missing"}.  If no method corresponds to the class of the
133
  argument, the evaluator looks for a method corresponding to the
134
  the superclasses (the other classes that the actual class
135
  extends, always including
136
  \code{"ANY"}).  If no match is found, the dispatch fails, with an
137
  error.  (But if there is a default method, that will always match.)
16764 jmc 138
 
23678 jmc 139
  If the match succeeds, it can find either a single method, or a
140
  methods list.  In the first case, the search is over, and returns
141
  the method.  In the second case, the search proceeds, with the
142
  next argument in the signature of the generic.  \emph{That} search
143
  may succeed or fail.  If it fails, the dispatch will try again with
144
  the next best match for the current argument, if there is one.
145
  The last match always corresponds to class \code{"ANY"}.
146
 
16764 jmc 147
The effect of this definition of the selection process is to order all
148
possible inherited methods, first by the superclasses for the first
149
argument, then within this by the superclasses for the second
150
argument, and so on.
23678 jmc 151
 
16764 jmc 152
}
23678 jmc 153
 
17000 jmc 154
\section{Changes with the Methods Package}{
25118 hornik 155
  The \pkg{methods} package is designed to leave other computations in R
19029 hornik 156
  unchanged.  There are, however, a few areas where the default
157
  functions and behavior are overridden when running with the methods
158
  package attached.  This section outlines those known to have some
159
  possible effect.
16559 jmc 160
 
19029 hornik 161
  \describe{
16559 jmc 162
 
19029 hornik 163
    \item{\code{class}:}{
25118 hornik 164
      The \pkg{methods} package enforces the notion that every object
165
      has a class; in particular, \code{class(x)} is never \code{NULL},
166
      as it would be for basic vectors, for example, when not using
167
      \pkg{methods}. 
16559 jmc 168
 
19029 hornik 169
      In addition, when assigning a class, the value is required to be a
170
      single string.  (However, objects can have multiple class names if
171
      these were generated by old-style class computations.  The methods
25118 hornik 172
      package does not hide the \dQuote{extra} class names.)
16559 jmc 173
 
19029 hornik 174
      Computations using the notion of \code{NULL} class attributes or
175
      of class attributes with multiple class names are not really
25118 hornik 176
      compatible with the ideas in the \pkg{methods} package.  Formal
177
      classes and class inheritance are designed to give more flexible
178
      and reliable implementations of similar ideas.
16559 jmc 179
 
19029 hornik 180
      If you do have to mix the two approaches, any operations that use
181
      class attributes in the old sense should be written in terms of
182
      \code{attr(x, "class")}, not \code{class(x)}.  In particular, test
183
      for no class having been assigned with
184
      \code{is.null(attr(x, "class"))}.
185
    }
16559 jmc 186
 
23443 hornik 187
    \item{Printing:}{
19029 hornik 188
      To provide appropriate printing automatically for objects with
25118 hornik 189
      formal class definitions, the \pkg{methods} package overrides
19029 hornik 190
      \code{print.default}, to look for methods for the generic function
191
      \code{show}, and to use a default method for objects with formal
192
      class definitions.
16764 jmc 193
 
19029 hornik 194
      The revised version of \code{print.default} is intended to produce
195
      identical printing to the original version for any object that
196
      does \emph{not} have a formally defined class, including honoring
197
      old-style print methods.  So far, no exceptions are known.
198
    }
199
  }
17000 jmc 200
}
15357 jmc 201
\references{
25118 hornik 202
  The R package \pkg{methods} implements, with a few exceptions, the
23678 jmc 203
  programming interface for classes
204
  and methods in the book \emph{Programming with Data} (John
205
  M. Chambers, Springer, 1998), in particular sections 1.6, 2.7, 2.8,
206
  and chapters 7 and 8.
15357 jmc 207
 
25118 hornik 208
  While the programming interface for the \pkg{methods} package follows
209
  the reference, the R software is an original implementation, so
210
  details in the reference that reflect the S4 implementation may appear
23678 jmc 211
  differently in R.  Also, there are extensions to the programming
212
  interface developed more recently than the reference.  For a
213
  discussion of details and ongoing development, see the web page 
214
  \url{http://developer.r-project.org/methodsPackage.html} and the
215
  pointers from that page.
15357 jmc 216
}
19029 hornik 217
\seealso{
218
  \code{\link{setGeneric}},
219
  \code{\link{setClass}}
220
}
15357 jmc 221
\keyword{programming}
222
\keyword{classes}
223
\keyword{methods}