Rev 68017 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
% File src/library/methods/man/implicitGeneric.Rd% Part of the R package, https://www.R-project.org% Copyright 1995-2007 R Core Team% Distributed under GPL 2 or later\name{implicitGeneric}\alias{implicitGeneric}\alias{setGenericImplicit}\alias{prohibitGeneric}\alias{registerImplicitGenerics}\alias{implicit generic}\title{Manage Implicit Versions of Generic Functions}\description{Create or access implicit generic functions, used to enforceconsistent generic versions of functions that are not currentlygeneric. Function \code{implicitGeneric()} returns the implicitgeneric version, \code{setGenericImplicit()} turns a generic implicit,\code{prohibitGeneric()} prevents your function from being madegeneric, and \code{registerImplicitGenerics()} saves a set of implicitgeneric definitions in the cached table of the current session.}\usage{implicitGeneric(name, where, generic)setGenericImplicit(name, where, restore = TRUE)prohibitGeneric(name, where)registerImplicitGenerics(what, where)}\arguments{\item{name}{ Character string name of the function.}\item{where}{ Package or environment in which to register the implicitgenerics. When using the functions from the top level of your ownpackage source, this argument can usually be omitted (and shouldbe).}\item{generic}{ Optionally, the generic function definition to becached, but usually omitted. See Details section.}\item{restore}{Should the non-generic version of the function berestored after the current.}\item{what}{For \code{registerImplicitGenerics()}, Optional table ofthe implicit generics to register, but nearly always omitted. SeeDetails section.}}\details{Multiple packages may define methods for the same function, using theversion of a function stored in one package. All these methods shouldbe marshaled and dispatched consistently when a user calls thefunction. For consistency, the generic version of the function musthave a unique definition (the same arguments allowed in methodssignatures, the same values for optional slots such as the valueclass, and the same standard or non-standard definition of thefunction itself).If the original function is already an S4 generic, there is noproblem. The implicit generic mechanism enforces consistency when theversion in the package owning the function is \emph{not} generic. Ifa call to \code{\link{setGeneric}()} attempts to turn a function inanother package into a generic, the mechanism compares the proposednew generic function to the implicit generic version of thatfunction. If the two agree, all is well. If not, and if the functionbelongs to another package, then the new generic will not beassociated with that package. Instead, a warning is issued and aseparate generic function is created, with its package slot set to thecurrent package, not the one that owns the non-generic version of thefunction. The effect is that the new package can still define methodsfor this function, but it will not share the methods in otherpackages, since it is forcing a different definition of the genericfunction.The right way to proceed in nearly all cases is to call\code{\link{setGeneric}("foo")}, giving \emph{only} the name of thefunction; this will automatically use the implicit generic version.If you don't like that version, the best solution is to convince theowner of the other package to agree with you and to insert code todefine the non-default properties of the function (even if the ownerdoes not want \code{foo()} to be a generic by default).For any function, the implicit generic form is a standard generic inwhich all formal arguments, except for \code{\dots}, are allowed inthe signature of methods. If that is the suitable generic for afunction, no action is needed. If not, the best mechanism is to set upthe generic in the code of the package owning the function, and tothen call \code{setGenericImplicit()} to record the implicit genericand restore the non-generic version. See the example.Note that the package can define methods for the implicit generic aswell; when the implicit generic is made a real generic, those methodswill be included.Other than predefining methods, the usual reason for having anon-default implicit generic is to provide a non-default signature,and the usual reason for \emph{that} is to allow lazy evaluation ofsome arguments. See the example. All arguments in the signature of ageneric function must be evaluated at the time the function needs toselect a method. (But those arguments can be missing, with or withouta default expression being defined; you can always examine\code{missing(x)} even for arguments in the signature.)If you want to completely prohibit anyone from turning your functioninto a generic, call \code{prohibitGeneric()}.}\value{Function \code{implicitGeneric()} returns the implicit genericdefinition (and caches that definition the first time if it has toconstruct it).The other functions exist for their side effect and return nothinguseful.}\seealso{\code{\link{setGeneric}}}\examples{### How we would make the function \link{with}() into a generic:## Since the second argument, 'expr' is used literally, we want## with() to only have "data" in the signature.## Note that 'methods'-internal code now has already extended with()## to do the equivalent of the following\dontrun{setGeneric("with", signature = "data")## Now we could predefine methods for "with" if we wanted to.## When ready, we store the generic as implicit, and restore the originalsetGenericImplicit("with")## (This example would only work if we "owned" function with(),## but it is in base.)}implicitGeneric("with")}\keyword{programming}\keyword{methods}