Rev 17454 | Blame | Last modification | View Log | Download | RSS feed
\name{Methods}\alias{Methods}\alias{plot}\title{General Information on Methods}\non_function{}\description{This documentation section covers some general topics on how methodswork and how the methods package interacts with the rest of R. Theinformation is usually not needed to get started with methods andclasses, but may be helpful for moderately ambitious projects, or whensomething doesn't work as expected.The section \bold{How Methods Work} describes the underlyingmechanism; \bold{Class Inheritance and Method Selection} provides moredetails on how class definitions determine which methods are used.The section \bold{Changes with the Methods Package} outlines possibleeffects on other computations when running with package \code{methods}.}\section{How Methods Work}{A generic function is a function that has associated with it acollection of other functions (the methods), all of which agree informal arguments with the generic. In R, the ``collection'' is anobject of class \code{"\link{MethodsList}"}, which contains a namedlist of methods (the \code{methods} slot), and the name of one of theformal arguments to the function (the \code{argument} slot). Thenames of the methods are the names of classes, and the correspondingelement defines the method or methods to be used if the correspondingargument has that class. For example, suppose a function \code{f} hasformal arguments \code{x} and \code{y}. The methods list object forthat function has the object \code{as.name("x")} as its\code{argument} slot. An element of the methods named \code{"track"}is selected if the actual argument corresponding to \code{x} is anobject of class \code{"track"}. If there is such an element, it cangenerally be either a function or another methods list object.In the first case, the function defines the method to use for any callin which \code{x} is of class \code{"track"}. In the second case, thenew methods list object defines the selection of methods depending onthe remaining formal arguments, in this example, \code{y}. The sameselection process takes place, recursively, using the new methods list.Eventually, the selection returns either a function or \code{NULL},meaning that no method matched the actual arguments.Each method selected corresponds conceptually to a \emph{signature};that is a named list of classes, with names corresponding to some orall of the formal arguments. In the previous example, if selectingclass \code{"track"} for \code{x}, finding that the selection wasanother methods list and then selecting class \code{"numeric"} for\code{y} would produce a method associated with the signature\code{x = "track", y = "numeric"}.The actual selection is done recursively, but you can see the methodsarranged by signature by calling the function\code{\link{showMethods}}, and objects with the methods arranged thisway (in two different forms) are returned by the functions\code{\link{listFromMlist}} and \code{\link{linearizeMlist}}.In an R session, each generic function has a single methods listobject defining all the currently available methods. The sessionmethods list object is created the first time the function is calledby merging all the relevant method definitions currently visible.Whenever something happens that might change the definitions (such asattaching or detaching a package with methods for this function, orexplicitly defining or removing methods), the merged methods listobject is removed. The next call to the function will recompute themerged definitions.When methods list are merged, they can come from two sources:\enumerate{\item {Methods list objects for the same function on one or morecurrently attached databases. These are merged so that methods ina database earlier in the search list override methods for thesame function later in the search list. A method overrides onlyanother method for the same signature. See the comments on class\code{"ANY"} in the section on \bold{Inheritance}.}\item {Methods list objects corresponding to the generic functionitself and to the group generic functions, if any, for thisfunction. Any generic function can be defined to belong to agroup generic. The methods for the group generic are available asmethods for this function. The group generic can itself bedefined as belong to a group; as a result there is a list of groupgeneric functions. A method defined for a function and aparticular signature overrides a method for the same signature forthat function's group generic.}}Merging is done first over databases for a particular function, andthen over the generic and its group generics.The result is a single methods list object that contains all themethods \emph{directly} defined for this function. As calls to thefunction occur, this information may be supplemented by\emph{inherited} methods, which we consider next.}\section{Class Inheritance and Method Selection}{If no method is found directly for the actual arguments in a call to ageneric function, an attempt is made to match the available methods tothe arguments by using \emph{inheritance}.Each class definition potentially includes the names of one or moreclasses that the new class extends. (These are sometimes called the\emph{superclasses} of the new class.) These classes themselves mayextend other classes. Putting all this information together producesthe full list of superclasses for this class. (You can see this listfor any class \code{"A"} from the expression \code{extends("A")}.)In addition, any class implicitly extends class \code{"ANY"}.A method will be selected by inheritance if we can find a method inthe methods list for a signature corresponding to anycombination of superclasses for each of the relevant arguments.The search for such a method is performed by the function\code{\link{MethodsListSelect}}, working as follows.For the first formal argument of the function, a list of classes ismade up from the class itself, all its superclasses, and class\code{"ANY"}.For each of these, the selection computation looks for an element ofthe methods with the corresponding name. Each time it finds one, itthen calls the selection process recursively if necessary to select amethod directly or by inheritance for the remaining arguments.Each one of these recursive calls can fail or it can return a function(the method). As long as the calls fail, the selection process moveson to the next superclass and tries again.The last step corresponds to class \code{"ANY"}, the default methoddefined at this level.The effect of this definition of the selection process is to order allpossible inherited methods, first by the superclasses for the firstargument, then within this by the superclasses for the secondargument, and so on.Superclasses are ordered by how direct they are: first, the directsuperclasses, then the superclasses of these classes.}\section{Changes with the Methods Package}{The methods package is designed to leave other computations in Runchanged. There are, however, a few areas where the defaultfunctions and behavior are overridden when running with the methodspackage attached. This section outlines those known to have somepossible effect.\describe{\item{\code{class}:}{The methods package enforces the notion that every object has aclass; in particular, \code{class(x)} is never \code{NULL}, as itwould be for basic vectors, for example, when not using methods.In addition, when assigning a class, the value is required to be asingle string. (However, objects can have multiple class names ifthese were generated by old-style class computations. The methodspackage does not hide the ``extra'' class names.)Computations using the notion of \code{NULL} class attributes orof class attributes with multiple class names are not reallycompatible with the ideas in the methods package. Formal classesand class inheritance are designed to give more flexible andreliable implementations of similar ideas.If you do have to mix the two approaches, any operations that useclass attributes in the old sense should be written in terms of\code{attr(x, "class")}, not \code{class(x)}. In particular, testfor no class having been assigned with\code{is.null(attr(x, "class"))}.}\item{Printing}{To provide appropriate printing automatically for objects withformal class definitions, the methods package overrides\code{print.default}, to look for methods for the generic function\code{show}, and to use a default method for objects with formalclass definitions.The revised version of \code{print.default} is intended to produceidentical printing to the original version for any object thatdoes \emph{not} have a formally defined class, including honoringold-style print methods. So far, no exceptions are known.}\item{\code{plot}}{A version of the \code{plot} function is included in the currentmethods package, differing from the one in the base package inthat it has a \code{y} argument (necessary if methods for plot areto be defined for the \code{y} data; see the examples for\code{\link{setMethod}}). This version will move into base assoon as it is tested.}}}\references{The web page \url{http://www.omegahat.org/RSMethods/index.html} is theprimary documentation.The functions in this package emulate the facility for classes andmethods described in \emph{Programming with Data} (John M. Chambers,Springer, 1998). See this book for further details and examples.}\author{John Chambers}\seealso{\code{\link{setGeneric}},\code{\link{setClass}}}\keyword{programming}\keyword{classes}\keyword{methods}