Define or Re-Define a Class of Objects

DESCRIPTION:

Define a new class or re-define an existing and save the necessary information in the metadata of an S chapter.

USAGE:

setClass(Class, representation, prototype, validity, access, where, version)

ARGUMENTS:

Class
character string name of the class to be defined.
representation
the representation for the class, including slot names and classes, and any contained classes. If representation is given, it should be a class representation (e.g., a call to the function representation), although the name of a single class is a valid short form.
prototype
The object to be used as the initial prototype. The function new will take this object, and set its class to Class. The prototype can be given in addition to or instead of a representation. In the latter case the class will not have slots defined.
validity
optional function of one argument for testing validity of an object from the class. Should return TRUE if its argument is a valid object from Class, or a character string describing the problem otherwise. This is called by the validObject function, which is called from new().
access
optional vector, naming the generic functions allowed access to the slots of the class.
where
database to hold the new definition. If a definition for Class already exists, this will be the default for where, if the database is writable; otherwise, where defaults to the working data. It is not allowed to redefine classes on a required library (library "main", e.g.).
version
optional flag. If version is TRUE, version information will also be saved in the meta-data (see ); by default, version management will be maintained if it's already started, but not started spontaneously.

VALUE:

the string Class, but with a special class that distinguishes it in the (very rare) situations where a string might be ambiguous, perhaps referring to a class, perhaps to an object from a class. If you want to be really safe, you can save this object and use it as the name of the class.

Side Effect:

Class is added to the known classes and to the metadata on database where.

SEE ALSO:

, , , .

EXAMPLES:

## a class with two numeric slots 
Track = setClass("track", representation(x="numeric", y="numeric")) 
## a class extending "matrix", with one new slot, and a validity method 
setClass("tsMatrix", representation("matrix", tsp = "numeric"), 
   validity = function(x) nrow(x) == length(x@tsp)) 
## a class extending "tsMatrix" with no additional slots 
setClass("tsMatrix2", "tsMatrix")