Rev 17454 | Rev 21345 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
\name{validObject}\alias{validObject}\alias{setValidity}\alias{getValidity}\title{ Test the Validity of an Object }\description{The validity of \code{object} related to its class definition istested. If the object is valid, \code{TRUE} is returned; otherwise,either a vector of strings describing validity failures is returned,or an error is generated (according to whether \code{test} is\code{TRUE}).The functions \code{getValidity} and \code{setValidity} get and setthe validity method of a class. This method is a function of oneobject that returns \code{TRUE} or a description of the non-validity.}\usage{validObject(object, test)getValidity(ClassDef)setValidity(ClassDef, method)}\arguments{\item{object}{ Any object, but not much will happen unless theobject's class has a formal definition.}\item{test}{ If \code{test} is \code{TRUE}, and validity fails thefunction returns a vector of strings describing the problems. If\code{test} is \code{FALSE} (the default) validity failure generatesan error.}\item{ClassDef}{The name of the class whose validity method is to beset.}\item{method}{A validity method; that is, either \code{NULL} or afunction of one argument (the \code{object}). Like\code{validObject}, the function should return \code{TRUE} if theobject is valid, and one or more descriptive strings if any problemsare found. Unlike \code{validObject}, it should never generate anerror.Note that validity methods do not have to check validity of anyslots or superclasses: the logic of \code{validObject} ensuresthese tests are done once only. As a consequence, if one validitymethod wants to use another, it should extract and call the methodfrom the other definition of the other class by calling\code{\link{getValidity}}: it should \emph{not} call\code{validObject}.}}\details{Validity testing takes place ``bottom up'': first the validity of theobject's slots, if any, is tested. Then for each of the classes thatthis class extends (the ``superclasses''), the explicit validitymethod of that class is called, if one exists. Finally, the validitymethod of \code{object}'s class is called, if there is one.Testing generally stops at the first stage of finding an error, exceptthat all the slots will be examined even if a slot has failed itsvalidity test.}\value{\code{validObject} returns \code{TRUE} if the object is valid.Otherwise a vector of strings describing problems found, except thatif \code{test} is \code{FALSE}, validity failure generates an error,with the corresponding strings in the error message.}\references{The web page \url{http://www.omegahat.org/RSMethods/index.html}is the primary documentation.The functions in this package emulate the facility for classesand methods described in \emph{Programming with Data} (JohnM. Chambers, Springer, 1998). See this book for further details andexamples.}\author{John Chambers}\seealso{ \code{\link{setClass}}. }\examples{setClass("track",representation(x="numeric", y = "numeric"))t1 <- new("track", x=1:10, y=sort(rnorm(10)))## A valid "track" object has the same number of x, y valuesvalidTrackObject <- function(x){if(length(x@x) == length(x@y)) TRUEelse paste("Unequal x,y lengths: ", length(x@x), ", ", length(x@y),sep="")}## assign the function as the validity method for the classsetValidity("track", validTrackObject)## t1 should be a valid "track" objectvalidObject(t1)## Now we do something badt1@x <- 1:20## This should generate an errortry(validObject(t1))}\keyword{programming}\keyword{classes}