Rev 35751 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
\name{representation}\alias{representation}\alias{prototype}\title{ Construct a Representation or a Prototype for a Class Definition}\description{In calls to\code{\link{setClass}}, these two functions construct,respectively, the \code{representation} and \code{prototype}arguments. They do various checks and handle special cases. You'reencouraged to use them when defining classes that, forexample, extend other classes as a data part or have multiplesuperclasses, or that combine extending a class and slots.}\usage{representation(...)prototype(...)}\arguments{\item{...}{The call to representation takes arguments that are single characterstrings. Unnamed arguments are classes that a newly defined classextends; named arguments name the explicit slots in the new class,and specify what class each slot should have.In the call to \code{prototype}, if an unnamed argument issupplied, it unconditionally forms the basis for the prototypeobject. Remaining arguments are taken to correspond to slots ofthis object. It is an error to supply more than one unnamed argument.}}\details{The \code{representation} function applies tests for the validity ofthe arguments. Each must specify the name of a class.The classes named don't have to exist when \code{representation} iscalled, but if they do, then the function will check for any duplicateslot names introduced by each of the inherited classes.The arguments to \code{prototype} are usually named initial valuesfor slots, plus an optional first argument that gives the objectitself. The unnamed argument is typically useful if there is a datapart to the definition (see the examples below).}\value{The value pf \code{representation} is just the list of arguments, after these have been checkedfor validity.The value of \code{prototype} is the object to be used as theprototype. Slots will have been set consistently with thearguments, but the construction does \emph{not} use the classdefinition to test validity of the contents (it hardly can, sincethe prototype object is usually supplied to create the definition).}\references{The R package \pkg{methods} implements, with a few exceptions, theprogramming interface for classes and methods in the book\emph{Programming with Data} (John M. Chambers, Springer, 1998), inparticular sections 1.6, 2.7, 2.8, and chapters 7 and 8.While the programming interface for the \pkg{methods} package followsthe reference, the R software is an original implementation, sodetails in the reference that reflect the S4 implementation may appeardifferently in R. Also, there are extensions to the programminginterface developed more recently than the reference. For adiscussion of details and ongoing development, see the web page\url{http://developer.r-project.org/methodsPackage.html} and thepointers from that page.}\seealso{ \code{\link{setClass}} }\examples{## representation for a new class with a directly define slot "smooth"## which should be a "numeric" object, and extending class "track"representation("track", smooth ="numeric")\dontshow{prev <- getClassDef("class3")setClass("class1", representation(a="numeric", b = "character"))setClass("class2", representation(a2 = "numeric", b = "numeric"))try(setClass("class3", representation("class1", "class2"))){if(is.null(prev))stopifnot(!isClass("class3"))elsestopifnot(identical(getClassDef("class3"), prev))}}setClass("Character",representation("character"))setClass("TypedCharacter",representation("Character",type="character"),prototype(character(0),type="plain"))ttt <- new("TypedCharacter", "foo", type = "character")\dontshow{stopifnot(identical(as(ttt, "character"), "foo"))}setClass("num1", representation(comment = "character"),contains = "numeric",prototype = prototype(pi, comment = "Start with pi"))\dontshow{stopifnot(identical(new("num1"), new("num1", pi, comment = "Start with pi")))for(cl in c("num1", "TypedCharacter", "Character", "class2", "class1"))removeClass(cl)}}\keyword{programming}\keyword{classes}