grapes-type-grapes: Types

Description Usage Arguments Details Examples

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

1
lhs %type% rhs

Arguments

lhs

an expression of the form:
[<parent-name>:]<type-name>([<slots>])
- <parent-name> optional, the name of the S4-class/type to inherit from, seperated by :
- <type-name> the name for the new type and constructor function.
- <slots> optional, name = value or name ~ type expressions. Name-Value expressions are used to construct a prototype. From the prototype the class of the slot will be inferred. They are also the defaults in the type constructor. Name-Type expressions define the classes of the slots. If no value (or type) is supplied, ANY is assumed.

rhs

the body of the initialize method as expression. It will be called with .Object and ... as arguments. .Object should be the return value. With .Object there is an instance of the type on which assertions can be formulated. Prior to the body (rhs) .Object <- callNextMethod() will be evaluated which enables proper initialization of your type and its inherited fields. See initialize for details.

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 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)

wahani/aoos documentation built on May 27, 2017, 11:54 p.m.