setClassUnion            package:methods            R Documentation

_C_l_a_s_s_e_s _D_e_f_i_n_e_d _a_s _t_h_e _U_n_i_o_n _o_f _O_t_h_e_r _C_l_a_s_s_e_s

_D_e_s_c_r_i_p_t_i_o_n:

     A class may be defined as the _union_ of other classes; that is,
     as a virtual class defined as a superclass of several other
     classes. Class unions are useful in method signatures or as slots
     in other classes, when we want to allow one of several classes to
     be supplied.

_U_s_a_g_e:

     setClassUnion(name, members, where)
     isClassUnion(Class)

_A_r_g_u_m_e_n_t_s:

    name: the name for the new union class. 

 members: the classes that should be members of this union.

   where: where to save the new class definition; by default, the
          environment of the package in which the 'setClassUnion' call
          appears, or the global environment if called outside of the
          source of a package.

   Class: the name or definition of a class. 

_D_e_t_a_i_l_s:

     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.

     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'.

_R_e_f_e_r_e_n_c_e_s:

     Chambers, John M. (2008) _Software for Data Analysis: Programming
     with R_ Springer.  (For the R version.)

     Chambers, John M. (1998) _Programming with Data_ Springer (For the
     original S4 version.)

_E_x_a_m_p_l_e_s:

     ## a class for either numeric or logical data
     setClassUnion("maybeNumber", c("numeric", "logical"))

     ## use the union as the data part of another class
     setClass("withId", representation("maybeNumber", id = "character"))

     w1 <- new("withId", 1:10, id = "test 1")
     w2 <- new("withId", sqrt(w1)%%1 < .01, id = "Perfect squares")

     ## add class "complex" to the union "maybeNumber"
     setIs("complex", "maybeNumber")

     w3 <- new("withId", complex(real = 1:10, imaginary = sqrt(1:10)))

     ## a class union containing the existing class  union "OptionalFunction"
     setClassUnion("maybeCode",
         c("expression", "language", "OptionalFunction"))

     is(quote(sqrt(1:10)), "maybeCode")  ## TRUE

