Rev 16764 | Rev 18976 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
\name{setGeneric}\alias{setGeneric}\alias{setGroupGeneric}\title{Define a New Generic Function}\description{Create a new generic function of the given name, for which formal methods canthen be defined.}\usage{setGeneric(name, def, group=NULL, valueClass=NULL, where=1, doAssign)setGroupGeneric(name, def, group=NULL, valueClass=NULL, knownMembers, where=1)}\arguments{\item{name}{ The character string name of the generic function. Inthe simplest and most common case, a function of this name isalready defined. The existing function may be non-generic oralready a generic (see the details).}\item{def}{ An optional function object, defining the generic. Thisargument is usually only needed (and is then required) if there isno current function of this name. In that case, the formal argumentsand default values for the generic are taken from \code{def}. Seealso argument \code{useAsDefault}.}\item{group}{ Optionally, a character string giving the group ofgeneric functions to which this function belongs. Methods can bedefined for the corresponding group generic, and these will thendefine methods for this specific generic function, if no method hasbeen explicitly defined for the corresponding signature. See thereferences for more discussion.}\item{valueClass}{ An optional character string defining the class toasserted to be given to the value returned by this genericfunction. By default, the generic function can return any object.(At the moment, the assertion supplied in this argument is notenforced or checked.)}\item{where}{ Where to store the resulting initial methods definition,and possibly the generic function; bydefault, stored into the global environment.}\item{doAssign}{ Should a new generic version of the function beassigned? The default action depends on whether a generic versioncurrently exists. If not, one will be assigned to \code{where},except that primitive functions in the base package are never turnedinto explicit generic functions (they are dispatched from theinternal code in the evaluator).You can supply \code{doAssign} as \code{TRUE} to force assigning ageneric function even if one already exists, or as \code{FALSE} toprevent assigning the generic in any case (for example, because youknow one exists on another package). But the rule of not assigninggenerics for primitives cannot be overriden by setting\code{doAssign} to \code{TRUE}.}\item{knownMembers}{(For \code{setGroupGeneric} only) The names of functions that are known to bemembers of this group. This information is used to reset cacheddefinitions of the member generics when information about the groupgeneric is changed.}}\details{The \code{setGeneric} function is called to initialize a genericfunction in an environment (usually the global environment), asprepartion for defining some methods for that function.The simplest and most common situation is that \code{name} is alreadya function, either an ordinary non-generic function or else a genericfunction in another environment. In the second case, essentially allyou will be doing is to make a copy of the existing generic, withoutits methods, in the current environment. \emph{Only} the \code{name}argument will be used. The new generic \emph{must} agree with thecurrent one in essentially everything except for the methods defined.The other simple and common situation is that you already have anon-generic function (probably in the same place you plan to save thegeneric), and now want to turn this function into a generic. In thiscase, too, you will most often supply only \code{name}. The existingfunction becomes the default method, and the special \code{group} and\code{valueClass} properties remain unspecified.The \code{setGroupGeneric} function behaves like \code{setGeneric} except thatit constructs a group generic function, differing in two ways from anordinary generic function. First,this function cannot be called directly, and the body of the functioncreated will contain a stop call with this information. Second, thegroup generic function contains information about the known members ofthe group, used to keep the members up to date when the groupdefinition changes, through changes in the search list or directspecification of methods, etc.}\value{The \code{setGeneric} function exists for its side effect: saving thegeneric function to allow methods to be specified later. It returns \code{name}.}\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 classes and methods described in\emph{Programming with Data}, (John M. Chambers, Springer, 1998). See this bookfor further details and examples.}\author{John Chambers}\seealso{\code{\link{Methods}} for a discussion of other functions tospecify and manipulate the methods of generic functions.}\keyword{ programming }\keyword{ methods }