The classes in members
must be defined before creating the
union. However, members can be added later on to an existing
union, as shown in the example below. Class unions can be
members of other class unions. The prototype object in the class union definition will be
NULL
if class "NULL"
is a member of the union and
the prototype object of the first member class otherwise (as of
version 2.15.0 of R; earlier versions had a NULL
prototype even if
that was not valid).
Class unions are the only way to create a class that is extended by
a class whose definition is sealed (for example, the
basic datatypes or other classes defined in the base or methods
package in R are sealed). You cannot say setIs("function", "other")
unless "other"
is a class union. In general, a
setIs
call of this form changes the definition of the
first class mentioned (adding "other"
to the list of
superclasses contained in the definition of "function"
).
Class unions get around this by not modifying the first class
definition, relying instead on storing information in the subclasses
slot of the class union. In order for this technique to work, the
internal computations for expressions such as
extends(class1, class2)
work
differently for class unions than for regular classes; specifically,
they test whether any class is in common between the superclasses of
class1
and the subclasses of class2
.
The different behavior for class unions is made possible because the
class definition object for class unions has itself a special class,
"ClassUnionRepresentation"
, an extension of class
classRepresentation
.