The R Project SVN R

Rev

Rev 56186 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
42333 ripley 1
% File src/library/methods/man/implicitGeneric.Rd
2
% Part of the R package, http://www.R-project.org
59039 ripley 3
% Copyright 1995-2007 R Core Team
42333 ripley 4
% Distributed under GPL 2 or later
5
 
42246 jmc 6
\name{implicitGeneric}
56186 murdoch 7
\alias{implicitGeneric}
42246 jmc 8
\alias{setGenericImplicit}
9
\alias{prohibitGeneric}
10
\alias{registerImplicitGenerics}
43625 ripley 11
\alias{implicit generic}
42251 ripley 12
\title{Manage Implicit Versions of Generic Functions}
42246 jmc 13
\description{
42251 ripley 14
  Create or access implicit generic functions, used to enforce
15
  consistent generic versions of functions that are not currently
16
  generic.  Function \code{implicitGeneric()} returns the implicit
17
  generic version, \code{setGenericImplicit()} turns a generic implicit,
18
  \code{prohibitGeneric()} prevents your function from being made
19
  generic, and \code{registerImplicitGenerics()} saves a set of implicit
20
  generic definitions in the cached table of the current session.
42246 jmc 21
}
22
\usage{
23
implicitGeneric(name, where, generic)
24
setGenericImplicit(name, where, restore = TRUE)
25
prohibitGeneric(name, where)
26
registerImplicitGenerics(what, where)
27
}
28
 
29
\arguments{
42251 ripley 30
  \item{name}{ Character string name of the function.}
31
  \item{where}{ Package or environment in which to register the implicit
32
    generics.  When using the functions from the top level of your own
33
    package source, this argument can usually be omitted (and should
34
    be).}
35
  \item{generic}{ Optionally, the generic function definition to be
36
    cached, but usually omitted.   See Details section.}
37
  \item{restore}{Should the non-generic version of the function be
38
    restored after the current.}
39
  \item{what}{For \code{registerImplicitGenerics()}, Optional table of
40
    the implicit generics to register, but nearly always omitted.   See
41
    Details section.}
42246 jmc 42
}
43
\details{
42251 ripley 44
  Multiple packages may define methods for the same function, using the
45
  version of a function stored in one package.  All these methods should
46
  be marshaled and dispatched consistently when a user calls the
47
  function.  For consistency, the generic version of the function must
48
  have a unique definition (the same arguments allowed in methods
49
  signatures, the same values for optional slots such as the value
50
  class, and the same standard or non-standard definition of the
51
  function itself).
42246 jmc 52
 
42251 ripley 53
  If the original function is already an S4 generic, there is no
54
  problem.  The implicit generic mechanism enforces consistency when the
55
  version in the package owning the function is \emph{not} generic.  If
56
  a call to \code{\link{setGeneric}()} attempts to turn a function in
57
  another package into a generic, the mechanism compares the proposed
58
  new generic function to the implicit generic version of that
59
  function. If the two agree, all is well. If not, and if the function
60
  belongs to another package, then the new generic will not be
61
  associated with that package.  Instead, a warning is issued and a
62
  separate generic function is created, with its package slot set to the
63
  current package, not the one that owns the non-generic version of the
64
  function. The effect is that the new package can still define methods
65
  for this function, but it will not share the methods in other
66
  packages, since it is forcing a different definition of the generic
67
  function.
42246 jmc 68
 
42251 ripley 69
  The right way to proceed in nearly all cases is to call
70
  \code{\link{setGeneric}("foo")}, giving \emph{only} the name of the
71
  function; this will automatically use the implicit generic version.
72
  If you don't like that version, the best solution is to convince the
73
  owner of the other package to agree with you and to insert code to
74
  define the non-default properties of the function (even if the owner
75
  does not want \code{foo()} to be a generic by default).
42246 jmc 76
 
42251 ripley 77
  For any function, the implicit generic form is a standard generic in
78
  which all formal arguments, except for \code{\dots}, are allowed in
79
  the signature of methods. If that is the suitable generic for a
80
  function, no action is needed. If not, the best mechanism is to set up
81
  the generic in the code of the package owning the function, and to
82
  then call \code{setGenericImplicit()} to record the implicit generic
83
  and restore the non-generic version.  See the example.
42246 jmc 84
 
42251 ripley 85
  Note that the package can define methods for the implicit generic as
86
  well; when the implicit generic is made a real generic, those methods
87
  will be included.
42246 jmc 88
 
42251 ripley 89
  Other than predefining  methods, the usual reason for having a
90
  non-default implicit generic is to provide a non-default signature,
91
  and the usual reason for \emph{that} is to allow lazy evaluation of
92
  some arguments.  See the example.  All arguments in the signature of a
93
  generic function must be evaluated at the time the function needs to
94
  select a method.  (But those arguments can be missing, with or without
95
  a default expression being defined; you can always examine
96
  \code{missing(x)} even for arguments in the signature.)
42246 jmc 97
 
42251 ripley 98
  If you want to completely prohibit anyone from turning your function
99
  into a generic, call \code{prohibitGeneric()}.
42246 jmc 100
}
101
\value{
42251 ripley 102
  Function \code{implicitGeneric()} returns the implicit generic
103
  definition (and caches that definition the first time if it has to
104
  construct it).
42389 maechler 105
 
42251 ripley 106
  The other functions exist for their side effect and return nothing
107
  useful.
42246 jmc 108
}
109
\seealso{\code{\link{setGeneric}}}
110
\examples{
42389 maechler 111
 
112
### How we would make the function \link{with}() into a generic:
113
 
114
## Since the second argument, 'expr' is used literally, we want
115
## with() to only have "data" in the signature.
116
 
117
## Note that 'methods'-internal code now has already extended  with()
118
## to do the equivalent of the following
42246 jmc 119
\dontrun{
120
setGeneric("with", signature = "data")
42389 maechler 121
## Now we could predefine methods for "with" if we wanted to.
42246 jmc 122
 
42389 maechler 123
## When ready, we store the generic as implicit, and restore the original
42246 jmc 124
setGenericImplicit("with")
125
 
42389 maechler 126
## (This example would only work if we "owned" function with(),
127
##  but it is in base.)}
42246 jmc 128
 
129
implicitGeneric("with")
130
}
42389 maechler 131
 
42246 jmc 132
\keyword{programming}
133
\keyword{methods}