The R Project SVN R

Rev

Rev 71228 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
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{
71540 jmc 62
 Chambers, John M. (2016)
63
 \emph{Extending R},
64
  Chapman & Hall.
65
(Chapters 9 and 10.)
25351 jmc 66
}
67
 
71540 jmc 68
 
25351 jmc 69
\examples{
70
## a class for either numeric or logical data
71
setClassUnion("maybeNumber", c("numeric", "logical"))
72
 
73
## use the union as the data part of another class
71228 maechler 74
setClass("withId", contains = "maybeNumber", slots = c(id = "character"))
25351 jmc 75
 
76
w1 <- new("withId", 1:10, id = "test 1")
77
w2 <- new("withId", sqrt(w1)\%\%1 < .01, id = "Perfect squares")
78
 
79
## add class "complex" to the union "maybeNumber"
80
setIs("complex", "maybeNumber")
81
 
82
w3 <- new("withId", complex(real = 1:10, imaginary = sqrt(1:10)))
83
 
84
## a class union containing the existing class  union "OptionalFunction"
85
setClassUnion("maybeCode",
86
    c("expression", "language", "OptionalFunction"))
87
 
88
is(quote(sqrt(1:10)), "maybeCode")  ## TRUE
26000 ripley 89
\dontshow{
25351 jmc 90
## The following test is less trivial than it looks.
91
## It depends on the assignment of the data part NOT performing a
92
## strict coerce to "numeric" on the way to satisfying
93
## is(ttt, "maybeNumber").
94
stopifnot(identical(w1@.Data, 1:10))
57838 jmc 95
removeClass("withId")
96
removeClass("maybeNumber")
25351 jmc 97
}
98
 
99
}
100
\keyword{programming}
101
\keyword{classes}