setGeneric
Create Generic Function

Description

Makes the function specified by name a valid generic function on the environment specified by where. If name is already a generic function on where, then setGeneric modifies the definition specified by def.

Usage

setGeneric(name, def = NULL, group = list(), valueClass = character(), 
           where = topenv(parent.frame()), package = NULL, signature = NULL, 
           useAsDefault = NULL, genericFunction = NULL, simpleInheritanceOnly = NULL)

Arguments

name a character string specifying the name of the generic function.
def the definition of the generic function. Typically, the function body consists of just one line: standardGeneric(name). If this argument is not given, a suitable function is constructed with arguments from the default method. (If there is no default function, this generates an error.)
group this argument is unimplemented, and a warning is generated if it is given.
valueClass this argument is unimplemented, and a warning is generated if it is given.
where the environment for storing the generic function definition.
package if package is the name of a loaded package, and def and useAsDefault are not given, the definition of name is read from this package.
signature a string vector. The arguments of the generic function that can be dispatched on. If this is not given, the signature is created from all the arguments of def.
useAsDefault
  • if this is a function, it is used as the default method of the generic.
  • If this is the value FALSE, then the generic has no default method.
  • If this is NULL, setGeneric searches for an existing function with the name of the generic and uses it as the default method if one is found.
genericFunction this argument is unimplemented, and a warning is generated if it is given.
simpleInheritanceOnly this argument is unimplemented, and a warning is generated if it is given.

Details

If the generic currently does not exist on where, setGeneric prepares an initial method definition and stores it. The default and usual case is that the function of the same name exists, and it is used as a template to infer the arguments to the generic and as a default method.
After this task, the ordinary object name on where is always the generic function definition, so that the apply family of functions work.
Value
returns the name of the generic.
See Also
setMethod.
Examples
# Define an S4 generic
setGeneric("myDim", function(object) standardGeneric("myDim"))

# Define S4 methods setMethod("myDim", "matrix", function(object) dim(object)) setMethod("myDim", "vector", function(object) length(object))

# call the generic myDim(matrix(1:4,2)) myDim(1:4)

Package methods version 6.1.4-13
Package Index