%g% {aoos} | R Documentation |
Wrapper for writing S4 generics and methods
Description
These are two wrappers around setGeneric
and setMethod
. A
relevant difference is that generics and methods are stored in the
environment in which %g%
and %m%
are called and not in the
top-environment. Furthermore both functions have side effects in that they
will call globalVariables
for the arguments and name of the
generic.
Usage
lhs %g% rhs
lhs %m% rhs
Arguments
lhs |
see details |
rhs |
the body as an expression |
Details
The Syntax for the left hand side:
[<valueClass>:]<genericName>(<argList>)
- valueClass
optional, is the class of the return value (see
setGeneric)
- genericName
the name of the generic function
- argList
are name = value
or name ~ type
expressions. Name-Value expressions are just like in a function definition.
Name-Type expressions are used to define the signature of a method (see
setMethod). See %type% and the examples how to work with
them.
Examples
# A new generic function and a method:
numeric : generic(x) %g% standardGeneric("generic")
generic(x ~ numeric) %m% x
generic(1)
# Polymorphic methods in an object:
Object <- function() {
numeric : generic(x) %g% standardGeneric("generic")
generic(x ~ numeric) %m% x
retList("Object")
}
Object()$generic(1)
# Class Unions:
## This generic allows for return values of type numeric or character:
'numeric | character' : generic(x) %g% standardGeneric("generic")
## This method also allows for numeric or character as argument:
generic(x ~ character | numeric) %m% x
generic(1)
generic("")