| %type% {aoos} | R Documentation |
Types
Description
This function can be used to define new S4-classes which are called Type.
They have an initialize method and in the introduced syntax init-method and
S4-class definition build a unit, hence a type. This simply captures a
typical setClass then setMethod("initialize", ...) pattern
where often some redundancy is introduced. The function has side effects due
to calling setClass, setMethod and assigning the constructor
function to the types name.
Usage
lhs %type% rhs
Arguments
lhs |
an expression of the form:
|
rhs |
the body of the initialize method as expression. It will be called
with |
Details
Name-Type expressions are also used in %m%. Besides this you
can formulate type unions in type expressions or the inheritance structure.
This has a side effect in that setClassUnion is called. Whenever you
write a type you can replace the name by an expression of the form:
type1 | type2. Outside the slots or argument list of a method these
expressions have to be quoted. In this example the following expression is
evaluated for you: setClassUnion("type1ORtype2", c("type1", "type2")).
Examples
# This will create an S4-class named 'Test' with two slots; x = "numeric"
# and y = "list"; prototype: list(x = 1, y = list()); and an initialize
# method where some checks are performed.
Test(x = 1, y = list()) %type% {
stopifnot(.Object@x > 0)
.Object
}
# This will create an S4-class named 'Numeric' with a slot and some tests.
numeric : Numeric(metaInfo = character()) %type% {
stopifnot(length(.Object) > 0)
stopifnot(all(.Object > 0))
.Object
}
# This will create an S4-class with slots, where the constructor function has
# no defaults. All slots will allow for ANY type.
Anything(x, y ~ ANY, z = NULL) %type% .Object
## Not run:
Anything() # error because x and y are missing
## End(Not run)
# Type Unions:
'character | numeric' : Either(either ~ character | numeric) %type% .Object
Either("", 1)