Rev 18976 | Rev 19473 | 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 formalmethods can then be defined. Typically, an existing non-genericfunction becomes the default method, but there is much optionalcontrol. See the details section.}\usage{setGeneric(name, def, group=NULL, valueClass=NULL, where=1, doAssign,myDispatch = FALSE, useAsDefault)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}. Thebody will be set to the standard dispatch mechanism, unless you wantto do something different (use argument \code{myDispatch} in thiscase.Note that \code{def} is \emph{not} the default method; use argument\code{useAsDefault} if you want to specify the default separately.}\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 vector or unevaluatedexpression. The value returned by the generic function musthave (or extend) this class, or one of the classes; otherwise,an error is generated. See thedetails section for supplying an expression.}\item{where}{ Where to store the resulting initial methods definition,and possibly the generic function; bydefault, stored into the global environment.}\item{doAssign}{You can supply \code{doAssign} as \code{TRUE} to force assigning ageneric function even if one already exists. See the detailssection. The rule of not assigninggenerics for primitives cannot be overriden by setting\code{doAssign} to \code{TRUE}.}\item{myDispatch}{Normally, the body of a generic function does nothing except todispatch methods (via a call to \code{\link{standardGeneric}}). Theevaluation model, however, allows the generic to do any othercomputation it wants, it it calls \code{standardGeneric} atsome point. Otherwise, \code{setGeneric} will ignore the body ofthe supplied generic unless \code{myDispatch} is set to\code{TRUE}. See the details section.}\item{useAsDefault}{Override the usual choice of default argument (an existingnon-generic function or no default if there is no such function).Argument \code{useAsDefault} can be supplied,either as a function to use for the default, or as a logical value.\code{FALSE} says not to have a default method at all, so that anerror occurs if there is not an explicit or inherited method for acall.\code{TRUE} says to use the existing function as default,unconditionally (hardly ever needed as an explicit argument).See the section on details.}\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), aspreparation for defining some methods for that function.The simplest and most common situation is that \code{name} is alreadyan ordinary non-generic function, and you now want to turn thisfunction into a generic.In thiscase 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.A second situation is that you want to create a new, genericfunction, unrelated to any existing function. In this case, youneed to supply a skeleton of the function definition, to define thearguments for the function. The body of a generic function isusually a standard form, \code{standardGeneric(name)} where\code{name} is the quoted name of the generic function.When calling \code{setGeneric} in this form, you would normallysupply the \code{def} argument as a function of this form. If nottold otherwise, \code{setGeneric} will try to find a non-genericversion of the function to use as a default. If you don't want thisto happen, supply the argument \code{useAsDefault}. That argumentcan be the function you want to be the default method. You can supplythe argument as \code{FALSE} to force no default (i.e., to cause an error ifthere is not direct or inherited method on call to the function).The same no-default situation occurs if there is no non-generic form of the function, and\code{useAsDefault} does not supply one. Remember, though, you canalso just assign the default you want (even one that generates anerror) rather than relying on the prior situation.Usually, calling \code{setGeneric} if there is already a\emph{generic} function of this name has no effect. If you want toforce a new definition, supply \code{doAssign = TRUE} (but it wouldbe cleaner in most cases to remove the old generic before creatingthe new one; see \code{\link{removeGeneric}}). There is oneabsolute restriction: you cannot create an explicit generic for theprimitive functions in the base library. These are dispatched fromC code for efficiency and, also, are implicitly not to be redefinedin any case.As mentioned, the body of a generic function usually does nothingexcept for dispatching methods by a call to \code{standardGeneric}.Under some circumstances you might just want to do some additionalcomputation in the generic function itself. As long as yourfunction eventually calls \code{standardGeneric} that is permissible(though perhaps not a good idea, in that it makes the behavior ofyour function different from the usual S model). If your functiondoes \emph{not} call \code{standardGeneric} you are in trouble,because none of the methods for the function will ever bedispatched. The computations in \code{setGeneric} look for such acall, and normally ignore the body of a \code{def} argument thathas no such call. If you really need to supply such a definition(perhaps because you call something else that calls\code{standardGeneric}), you can force the use of \code{def} bysupplying \code{myDispatch = TRUE}.By default, the generic function can return any object.If \code{valueClass} is supplied, it can be either a vector ofclass names or an unevaluated expression. If an unevaluated expression is supplied (e.g., by using\code{\link{quote}}), the expression will be evaluated in the frameof the call to the generic function, after the method isdispatched. Therefore, the expression can involve any argument orlocally defined object in that frame.The test is done in the form\code{is(object, class)}, so that a value from a class that extendsa supplied value class will be accepted.An empty (i.e., zero length) vector of classes means anything isallowed (but it would be clearer programming to have an expression thatevaluated to \code{"ANY"}).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 theprimary documentation.The functions in this package emulate the facility for classes andmethods described in \emph{Programming with Data} (John M. Chambers,Springer, 1998). See this book for further details and examples.}\author{John Chambers}\seealso{\code{\link{Methods}} for a discussion of other functions to specifyand manipulate the methods of generic functions.}\keyword{ programming }\keyword{ methods }