Rev 38632 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
\name{class}\title{Object Classes}\alias{class}\alias{class<-}\alias{oldClass}\alias{oldClass<-}\alias{unclass}\alias{inherits}\description{\R possesses a simple generic function mechanism which can be used foran object-oriented style of programming. Method dispatch takes placebased on the class of the first argument to the generic function.}\usage{class(x)class(x) <- valueunclass(x)inherits(x, what, which = FALSE)oldClass(x)oldClass(x) <- value}\arguments{\item{x}{a \R object}\item{what, value}{a character vector naming classes. \code{value}can also be \code{NULL}.}\item{which}{logical affecting return value: see Details.}}\details{Many \R objects have a \code{class} attribute, a character vectorgiving the names of the classes which the object \dQuote{inherits}from. If the object does not have a class attribute, it has animplicit class, \code{"matrix"}, \code{"array"} or the result of\code{\link{mode}(x)}. (Functions \code{oldClass} and\code{oldClass<-} get and set the attribute, which can also be donedirectly.)When a genericfunction \code{fun} is applied to an object with class attribute\code{c("first", "second")}, the system searches for a function called\code{fun.first} and, if it finds it, applies it to the object. If nosuch function is found, a function called \code{fun.second} is tried.If no class name produces a suitable function, the function\code{fun.default} is used (if it exists).If there is no class attribute, the implicit class is tried, then thedefault method.The function \code{class} prints the vector of names of classes anobject inherits from. Correspondingly, \code{class<-} sets theclasses an object inherits from. Assigning a zero-length vector or\code{NULL} removes the class attribute.\code{unclass} returns (a copy of) its argument with its classattribute removed. (It is not allowed for objects which cannot becopied, namely environments and external pointers.)\code{inherits} indicates whether its first argument inherits from anyof the classes specified in the \code{what} argument. If \code{which}is \code{TRUE} then an integer vector of the same length as\code{what} is returned. Each element indicates the position in the\code{class(x)} matched by the element of \code{what}; zero indicatesno match. If \code{which} is \code{FALSE} then \code{TRUE} isreturned by \code{inherits} if any of the names in \code{what} matchwith any \code{class}.}\note{Functions \code{oldClass} and \code{oldClass<-} behave in the same wayas functions of those names in S-PLUS 5/6, \emph{but} in \R\code{\link{UseMethod}} dispatches on the class as returned by\code{class} (with some interpolated classes: see the link) ratherthan \code{oldClass}. \emph{However}, \link{group generic}s dispatchon the \code{oldClass} for efficiency.}\section{Formal classes}{An additional mechanism of \emph{formal} classes is available inpackages \pkg{methods} which is attached by default. For objectswhich have a formal class, its name is returned by \code{class}as a character vector of length one.The replacement version of the function sets the class to the valueprovided. For classes that have a formal definition, directlyreplacing the class this way is strongly deprecated. The expression\code{\link[methods]{as}(object, value)} is the way to coerce an object to aparticular class.}\seealso{\code{\link{UseMethod}}, \code{\link{NextMethod}},\code{\link{group generic}}.}\examples{x <- 10inherits(x, "a") #FALSEclass(x) <- c("a", "b")inherits(x,"a") #TRUEinherits(x, "a", TRUE) # 1inherits(x, c("a", "b", "c"), TRUE) # 1 2 0}\keyword{methods}\keyword{classes}