| 42333 |
ripley |
1 |
% File src/library/methods/man/setClassUnion.Rd
|
| 68948 |
ripley |
2 |
% Part of the R package, https://www.R-project.org
|
| 71228 |
maechler |
3 |
% Copyright 1995-2016 R Core Team
|
| 42333 |
ripley |
4 |
% Distributed under GPL 2 or later
|
|
|
5 |
|
| 25351 |
jmc |
6 |
\name{setClassUnion}
|
| 56186 |
murdoch |
7 |
\alias{setClassUnion}
|
| 25351 |
jmc |
8 |
\alias{isClassUnion}
|
|
|
9 |
\alias{ClassUnionRepresentation-class}
|
|
|
10 |
\title{Classes Defined as the Union of Other Classes}
|
|
|
11 |
\description{
|
|
|
12 |
A class may be defined as the \emph{union} of other classes; that
|
|
|
13 |
is, as a virtual class defined as a superclass of several other
|
|
|
14 |
classes. Class unions are useful in method signatures or as slots in
|
|
|
15 |
other classes, when we want to allow one of several classes to be supplied.
|
|
|
16 |
}
|
|
|
17 |
\usage{
|
|
|
18 |
setClassUnion(name, members, where)
|
|
|
19 |
isClassUnion(Class)
|
|
|
20 |
}
|
|
|
21 |
\arguments{
|
|
|
22 |
\item{name}{ the name for the new union class. }
|
| 71540 |
jmc |
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. In calls from
|
|
|
25 |
a package's source code, should be omitted to save the definition
|
|
|
26 |
in the package's namespace.}
|
| 25351 |
jmc |
27 |
|
|
|
28 |
|
|
|
29 |
\item{Class}{ the name or definition of a class.
|
|
|
30 |
}
|
|
|
31 |
}
|
|
|
32 |
\details{
|
|
|
33 |
The classes in \code{members} must be defined before creating the
|
|
|
34 |
union. However, members can be added later on to an existing
|
|
|
35 |
union, as shown in the example below. Class unions can be
|
|
|
36 |
members of other class unions.
|
|
|
37 |
|
| 71540 |
jmc |
38 |
|
|
|
39 |
Class unions are the only way to create a new superclass of
|
|
|
40 |
a class whose definition is sealed. The namespace of all
|
|
|
41 |
packages is sealed when the package is loaded, protecting the
|
|
|
42 |
class and other definitions from being overwritten from another
|
|
|
43 |
class or from the global environment. A call to
|
|
|
44 |
\code{\link{setIs}} that tried to define a new superclass for
|
|
|
45 |
class \code{"numeric"}, for example, would cause an error.
|
| 58548 |
jmc |
46 |
|
| 71540 |
jmc |
47 |
Class unions are the exception; the class union
|
|
|
48 |
\code{"maybeNumber"} in the examples defines itself as a new
|
|
|
49 |
superclass of \code{"numeric"}. Technically, it does not alter the
|
|
|
50 |
metadata object in the other package's namespace and, of course,
|
|
|
51 |
the effect of the class union depends on loading the package it
|
|
|
52 |
belongs to. But, basically, class unions are sufficiently useful
|
|
|
53 |
to justify the exemption.
|
| 25351 |
jmc |
54 |
|
|
|
55 |
The different behavior for class unions is made possible because the
|
|
|
56 |
class definition object for class unions has itself a special class,
|
| 44751 |
maechler |
57 |
\code{"ClassUnionRepresentation"}, an extension of class
|
|
|
58 |
\code{\linkS4class{classRepresentation}}.
|
| 25351 |
jmc |
59 |
}
|
|
|
60 |
|
|
|
61 |
\references{
|
| 88585 |
hornik |
62 |
\bibshow{R:Chambers:2016}
|
|
|
63 |
(Chapters 9 and 10.)
|
| 25351 |
jmc |
64 |
}
|
|
|
65 |
|
| 71540 |
jmc |
66 |
|
| 25351 |
jmc |
67 |
\examples{
|
|
|
68 |
## a class for either numeric or logical data
|
|
|
69 |
setClassUnion("maybeNumber", c("numeric", "logical"))
|
|
|
70 |
|
|
|
71 |
## use the union as the data part of another class
|
| 71228 |
maechler |
72 |
setClass("withId", contains = "maybeNumber", slots = c(id = "character"))
|
| 25351 |
jmc |
73 |
|
|
|
74 |
w1 <- new("withId", 1:10, id = "test 1")
|
|
|
75 |
w2 <- new("withId", sqrt(w1)\%\%1 < .01, id = "Perfect squares")
|
|
|
76 |
|
|
|
77 |
## add class "complex" to the union "maybeNumber"
|
|
|
78 |
setIs("complex", "maybeNumber")
|
|
|
79 |
|
|
|
80 |
w3 <- new("withId", complex(real = 1:10, imaginary = sqrt(1:10)))
|
|
|
81 |
|
|
|
82 |
## a class union containing the existing class union "OptionalFunction"
|
|
|
83 |
setClassUnion("maybeCode",
|
|
|
84 |
c("expression", "language", "OptionalFunction"))
|
|
|
85 |
|
|
|
86 |
is(quote(sqrt(1:10)), "maybeCode") ## TRUE
|
| 26000 |
ripley |
87 |
\dontshow{
|
| 25351 |
jmc |
88 |
## The following test is less trivial than it looks.
|
|
|
89 |
## It depends on the assignment of the data part NOT performing a
|
|
|
90 |
## strict coerce to "numeric" on the way to satisfying
|
|
|
91 |
## is(ttt, "maybeNumber").
|
|
|
92 |
stopifnot(identical(w1@.Data, 1:10))
|
| 57838 |
jmc |
93 |
removeClass("withId")
|
|
|
94 |
removeClass("maybeNumber")
|
| 25351 |
jmc |
95 |
}
|
|
|
96 |
|
|
|
97 |
}
|
|
|
98 |
\keyword{programming}
|
|
|
99 |
\keyword{classes}
|