The R Project SVN R

Rev

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

Rev 71366 Rev 71909
Line 1... Line 1...
1
% File src/library/methods/man/setClass.Rd
1
% File src/library/methods/man/setClass.Rd
2
% Part of the R package, https://www.R-project.org
2
% Part of the R package, https://www.R-project.org
3
% Copyright 1995-2013 R Core Team
3
% Copyright 1995-2017 R Core Team
4
% Distributed under GPL 2 or later
4
% Distributed under GPL 2 or later
5
 
5
 
6
\name{setClass}
6
\name{setClass}
7
\alias{setClass}
7
\alias{setClass}
8
\alias{classGeneratorFunction-class}
8
\alias{classGeneratorFunction-class}
9
\title{Create a Class Definition}
9
\title{Create a Class Definition}
10
\description{
10
\description{
11
  Create  a class definition and return a generator function to create
11
  Create  a class definition and return a generator function to create
12
  objects from the class.  Standard usage will be
12
  objects from the class.  Typical usage will be
13
  of the form:
13
  of the style:
14
 
14
 
15
   \code{setClass(Class, slots= , contains =)}
15
   \code{myClass <- setClass("myClass", slots= ...., contains =....)}
16
 
16
 
17
 where \code{Class} is the name of the new class and, if supplied, the arguments    
17
 where the first argument is the name of the new class and, if supplied, the arguments    
18
  \code{slots=} and \code{contains=} specify the slots
18
  \code{slots=} and \code{contains=} specify the slots
19
  in the new class and existing classes from which the new class
19
  in the new class and existing classes from which the new class
20
  should inherit.  Calls to \code{setClass()} are normally found in the
20
  should inherit.  Calls to \code{setClass()} are normally found in the
21
  source of a package; when the package is loaded the class will be
21
  source of a package; when the package is loaded the class will be
22
  defined in the package's namespace.
22
  defined in the package's namespace.  Assigning the generator
-
 
23
  function with the name of the class is  convenient for users, but
-
 
24
  not a requirement.
23
}
25
}
24
\usage{
26
\usage{
25
setClass(Class, representation, prototype, contains=character(),
27
setClass(Class, representation, prototype, contains=character(),
26
         validity, access, where, version, sealed, package,
28
         validity, access, where, version, sealed, package,
27
         S3methods = FALSE, slots)
29
         S3methods = FALSE, slots)
28
}
30
}
29
\arguments{
31
\arguments{
30
  \item{Class}{character string name for the class.}
32
  \item{Class}{character string name for the class.}
31
  \item{slots}{  A named vector with the names being those of the slots in
33
  \item{slots}{  The names and classes for the slots in the new class.  This argument
32
      the new class.  Each element of the vector specifies an
-
 
33
      existing class; the corresponding slot must be from this class
-
 
34
      or a subclass of it.  This argument
-
 
35
      must be supplied by name, \code{slots=}, in the call, for back compatibility
34
      must be supplied by name, \code{slots=}, in the call, for back compatibility
36
      with other arguments no longer recommended.
35
      with other arguments no longer recommended.
37
 
36
 
-
 
37
      The argument must be  vector with a names attribute, the names being those of the slots in
-
 
38
      the new class.  Each element of the vector specifies an
-
 
39
      existing class; the corresponding slot must be from this class
-
 
40
      or a subclass of it.  Usually, this is a character vector
-
 
41
      naming the classes.  It's also legal for the elements of the
-
 
42
      vector to be class representation objects, as returned by \code{\link{getClass}}.
-
 
43
 
38
 
44
 
-
 
45
    As a limiting
39
    It is allowed to provide an unnamed character vector as a limiting
46
    case,  the argument may be an unnamed character
40
    case, with the elements taken as slot names and all slots having
47
    vector;  the elements are  taken as slot names and all slots have
41
    the unrestricted class \code{"ANY"}.
48
    the unrestricted class \code{"ANY"}. 
42
  }
49
  }
43
  \item{contains}{ A vector specifying existing classes from which
50
  \item{contains}{ A vector specifying existing classes from which
44
      this class should inherit. The new class will have all the slots
51
      this class should inherit. The new class will have all the slots
45
      of the superclasses, with the same requirements on the classes
52
      of the superclasses, with the same requirements on the classes
46
      of these slots.  This argument
53
      of these slots.  This argument
Line 99... Line 106...
99
  returned, invisibly.  A call to this function generates a call to
106
  returned, invisibly.  A call to this function generates a call to
100
  \code{\link{new}} for the class.  The call takes any number of arguments,
107
  \code{\link{new}} for the class.  The call takes any number of arguments,
101
  which will be passed on to the initialize method.  If no
108
  which will be passed on to the initialize method.  If no
102
  \code{initialize} method is defined for the class or one of its
109
  \code{initialize} method is defined for the class or one of its
103
  superclasses, the default method expects named arguments with the
110
  superclasses, the default method expects named arguments with the
-
 
111
  name of one of the slots and unnamed arguments that are objects from
104
  name of one of the slots.
112
  one of the contained classes.
105
 
113
 
106
  Typically the generator function is assigned the name of the class,
114
  Typically the generator function is assigned the name of the class,
107
  for programming clarity.  This is not a requirement and objects
115
  for programming clarity.  This is not a requirement and objects
108
  from the class can also be generated directly from
116
  from the class can also be generated directly from
109
  \code{\link{new}}.  The advantages of the generator function are a
117
  \code{\link{new}}.  The advantages of the generator function are a
Line 130... Line 138...
130
 
138
 
131
 
139
 
132
The slots in a class definition will be the union of all the slots
140
The slots in a class definition will be the union of all the slots
133
specified directly by \code{slots} and all the slots in all
141
specified directly by \code{slots} and all the slots in all
134
the contained classes.
142
the contained classes.
135
There can only be one slot with a given name; specifically, the
143
There can only be one slot with a given name.
136
direct and inherited slot names must be unique.
144
A class may override the definition of a slot with a given name, but
137
That does not, however, prevent the same class from being inherited
145
\emph{only} if the newly specified class is a subclass of the
138
via more than one path.
146
inherited one.
-
 
147
For example, if the contained class had a slot \code{a} with class
-
 
148
\code{"ANY"}, then a subclass could specify \code{a} with class
-
 
149
\code{"numeric"},
-
 
150
but if the original specification for the slot was class
-
 
151
\code{"character"}, the new call to \code{setClass} would generate an error.
139
 
152
 
140
 
153
 
141
 
154
 
142
  Slot names \code{"class"} and \code{"Class"} are not allowed.
155
  Slot names \code{"class"} and \code{"Class"} are not allowed.
143
  There are other slot names with a special meaning; these names start with
156
  There are other slot names with a special meaning; these names start with
Line 218... Line 231...
218
These can not be the types for objects with user-defined
231
These can not be the types for objects with user-defined
219
classes (either S4 or S3) because setting an attribute overwrites the
232
classes (either S4 or S3) because setting an attribute overwrites the
220
object in all contexts.
233
object in all contexts.
221
It is possible to define a class that inherits from such types,
234
It is possible to define a class that inherits from such types,
222
through an indirect mechanism that stores the inherited object in a
235
through an indirect mechanism that stores the inherited object in a
223
reserved slot.
236
reserved slot, \code{".xData"}.
224
See the
237
See the
225
example for class \code{"stampedEnv"} below.
238
example for class \code{"stampedEnv"} below.
-
 
239
An object from such a class does \emph{not} have a \code{".Data"} pseudo-slot.
-
 
240
 
-
 
241
For most computations, these classes behave transparently as if they
-
 
242
inherited directly from the anomalous type.
226
S3 method dispatch and the relevant \code{as.}\emph{type}\code{()}
243
S3 method dispatch and the relevant \code{as.}\emph{type}\code{()}
227
functions should behave correctly, but code that uses the type of the
244
functions should behave correctly, but code that uses the type of the
228
object directly will not.
245
object directly will not.
-
 
246
For example, \code{as.environment(e1)} would work as expected with the
-
 
247
\code{"stampedEnv"} class, but \code{typeof(e1)} is \code{"S4"}.
229
 
248
 
230
}
249
}
231
 
250
 
232
\section{Inheriting from S3 Classes}{
251
\section{Inheriting from S3 Classes}{
233
Old-style S3 classes have no formal definition.  Objects are
252
Old-style S3 classes have no formal definition.  Objects are