Rev 71366 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
% File src/library/methods/man/Methods_for_S3.Rd% Part of the R package, https://www.R-project.org% Copyright 1995-2016 R Core Team% Distributed under GPL 2 or later\name{Methods_for_S3}\alias{Methods_for_S3}\title{Methods For S3 and S4 Dispatch}\description{The S3 and S4 software in \R are two generations implementingfunctional object-oriented programming.S3 is the original, simpler for initial programming but less general,less formal and less open to validation.The S4 formal methods and classes provide these features but requiremore programming.In modern \R, the two versions attempt to work together. Thisdocumentation outlines how to write methods for both systems bydefining an S4 method for a function that dispatches S3 methods.The systems can also be combined by using an S3 class with S4 methoddispatch or in S4 class definitions. See \code{\link{setOldClass}}.}\section{S3 Method Dispatch}{The \R evaluator will \sQuote{dispatch} a method from a function calleither when the body of the function calls the special primitive\code{\link{UseMethod}} or when the call is to one of the builtinprimitives such as the \code{math} functions or the binary operators.S3 method dispatch looks at the class of the firstargument or the class of eitherargument in a call to one of the primitive binary operators.In pure S3 situations, \sQuote{class} in this context means the classattribute or the implied class for a basic data type such as\code{"numeric"}.The first S3 method that matches a name in the class is called and thevalue of that call is the value of the original function call.For details, see \link{S3Methods}.In modern \R, a function \code{meth} in a package is registered as an S3 methodfor function \code{fun} and class \code{Class} byincluding in the package's \code{NAMESPACE} file the directive\code{S3method(fun, Class, meth)}By default (and traditionally), the third argument is taken to be thefunction \code{fun.Class}; that is,the name of thegeneric function, followed by \code{"."}, followed by the name of theclass.As with S4 methods, a method that has been registered will be added toa table of methods for this function when the corresponding package isloaded into the session.Older versions of \R, copying the mechanism in S, looked for themethod in the current search list, but packages should now alwaysregister S3 methods rather than requiring the package to be attached.}\section{Methods for S4 Classes}{Two possible mechanisms for implementing a method corresponding to anS4 class, there are two possibilities are to register it as an S3 method with theS4 class name or to define and set an S4 method, which will have theside effect of creating an S4 generic version of this function.For most situations either works, butthe recommended approach is to do both: register the S3 method and supply theidentical function as the definition of the S4 method.This ensures that the proposed method will be dispatched for anyapplicable call to the function.As an example, suppose an S4 class \code{"uncased"} is defined,extending \code{"character"} and intending to ignore upper- andlower-case.The base function \code{\link{unique}} dispatches S3 methods.To define the class and a method for this function:\code{setClass("uncased", contains = "character")}\code{unique.uncased <- function(x, incomparables = FALSE, ...)nextMethod(tolower(x))}\code{setMethod("unique", "uncased", unique.uncased)}In addition, the \code{NAMESPACE} for the package should contain:\code{S3method(unique, uncased)}\code{exportMethods(unique)}The result is to define identical S3 and S4 methods and ensure that allcalls to \code{unique} will dispatch that method when appropriate.}\section{Details}{The reasons for defining both S3 and S4 methods are as follows:\enumerate{\item An S4 method alone will not be seen if the S3 generic functionis called directly. This will be the case, for example, if somefunction calls \code{unique()} from a package that does not makethat function an S4 generic.However, primitive functions and operatorsare exceptions: The internal C code will look for S4 methodsif and only if the object is an S4 object. S4 method dispatchwould be used to dispatch any binary operator calls where eitherof the operands was an S4 object, for example.\item An S3 method alone will not be called if there is \emph{any}eligible non-default S4 method.So if a package defined an S3method for \code{unique} for an S4 class but another packagedefined an S4 method for a superclass of that class, thesuperclass method would be chosen, probably not what wasintended.}S4 and S3 method selection are designed to follow compatible rules ofinheritance, as far as possible.S3 classes can be used for any S4 method selection, provided that theS3 classes have been registered by a call to\code{\link{setOldClass}}, with that call specifying the correct S3inheritance pattern.S4 classes can be used for any S3 method selection; when an S4 objectis detected, S3 method selection uses the contents of\code{\link{extends}(class(x))} as the equivalent of the S3inheritance (the inheritance is cached after the first call).For the details of S4 and S3dispatch see \link{Methods_Details} and \link{S3Methods}.}\references{Chambers, John M. (2016)\emph{Extending R},Chapman & Hall.(Chapters 9 and 10.)}