The R Project SVN R

Rev

Rev 71228 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 71228 Rev 71540
Line 18... Line 18...
18
setClassUnion(name, members, where)
18
setClassUnion(name, members, where)
19
isClassUnion(Class)
19
isClassUnion(Class)
20
}
20
}
21
\arguments{
21
\arguments{
22
  \item{name}{ the name for the new union class. }
22
  \item{name}{ the name for the new union class. }
23
  \item{members}{ the classes that should be members of this union.}
23
  \item{members}{ the names of the classes that should be members of this union.}
24
  \item{where}{ where to save the new class definition; by default,
24
  \item{where}{ where to save the new class definition.  In calls from
25
      the environment of the package in which the \code{setClassUnion}
-
 
26
      call appears, or the global environment if called outside of the
25
    a package's source code, should be omitted to save the definition
27
      source of a package.}
26
    in the package's namespace.}
28
 
27
 
29
 
28
 
30
  \item{Class}{ the name or definition of a class.
29
  \item{Class}{ the name or definition of a class.
31
    }
30
    }
32
}
31
}
Line 34... Line 33...
34
  The classes in \code{members} must be defined before creating the
33
  The classes in \code{members} must be defined before creating the
35
      union.  However, members can be added later on to an existing
34
      union.  However, members can be added later on to an existing
36
      union, as shown in the example below. Class unions can be
35
      union, as shown in the example below. Class unions can be
37
      members of other class unions.
36
      members of other class unions.
38
 
37
 
39
      The prototype object in the class union definition will be
-
 
40
      \code{NULL} if class \code{"NULL"} is a member of the union and
-
 
41
      the prototype object of the first member class otherwise (as of
-
 
42
      version 2.15.0 of R; earlier versions had a \code{NULL} prototype even if
-
 
43
      that was not valid).
-
 
44
 
38
 
45
  Class unions are the only way to create a class that is extended by
39
  Class unions are the only way to create a new superclass of
46
      a class whose definition is sealed (for example, the
40
      a class whose definition is sealed.  The namespace of all
47
      basic datatypes or other classes defined in the base or methods
41
      packages is sealed when the package is loaded, protecting the
48
      package in R are sealed).  You cannot say \code{setIs("function", "other")}
42
      class and other definitions from being overwritten from another
49
      unless \code{"other"} is a class union.  In general, a
43
      class or from the global environment.  A call to
50
      \code{setIs} call of this form changes the definition of the
44
      \code{\link{setIs}} that tried to define a new superclass for
51
      first class mentioned (adding \code{"other"} to the list of
-
 
52
      superclasses contained in the definition of \code{"function"}).
45
      class \code{"numeric"}, for example, would cause an error.
53
 
46
 
54
      Class unions get around this by not modifying the first class
47
   Class unions are the exception; the class union
55
  definition, relying instead on storing information in the subclasses
48
   \code{"maybeNumber"} in the examples defines itself as a new
56
  slot of the class union.  In order for this technique to work, the
49
   superclass of \code{"numeric"}.  Technically, it does not alter the
57
  internal computations for expressions such as
50
   metadata object in the other package's namespace and, of course,
58
  \code{\link{extends}(class1, class2)} work
-
 
59
  differently for class unions than for regular classes; specifically,
51
   the effect of the class union depends on loading the package it
60
  they test whether any class is in common between the superclasses of
52
   belongs to.  But, basically, class unions are sufficiently useful
61
  \code{class1} and the subclasses of \code{class2}.
53
   to justify the exemption.
62
 
54
 
63
  The different behavior for class unions is made possible because the
55
  The different behavior for class unions is made possible because the
64
  class definition object for class unions has itself a special class,
56
  class definition object for class unions has itself a special class,
65
  \code{"ClassUnionRepresentation"}, an extension of class
57
  \code{"ClassUnionRepresentation"}, an extension of class
66
  \code{\linkS4class{classRepresentation}}.
58
  \code{\linkS4class{classRepresentation}}.
67
  }
59
  }
68
 
60
 
69
\references{
61
\references{
70
 Chambers, John M. (2008)
62
 Chambers, John M. (2016)
71
 \emph{Software for Data Analysis: Programming with R}
63
 \emph{Extending R},
72
  Springer.  (For the R version.)
64
  Chapman & Hall.
73
 
-
 
74
 Chambers, John M. (1998)
65
(Chapters 9 and 10.)
75
 \emph{Programming with Data}
-
 
76
 Springer (For the original S4 version.)
-
 
77
}
66
}
78
 
67
 
-
 
68
 
79
\examples{
69
\examples{
80
## a class for either numeric or logical data
70
## a class for either numeric or logical data
81
setClassUnion("maybeNumber", c("numeric", "logical"))
71
setClassUnion("maybeNumber", c("numeric", "logical"))
82
 
72
 
83
## use the union as the data part of another class
73
## use the union as the data part of another class