| Line 4... |
Line 4... |
| 4 |
\alias{initialize-methods}
|
4 |
\alias{initialize-methods}
|
| 5 |
\alias{initialize,ANY-method}
|
5 |
\alias{initialize,ANY-method}
|
| 6 |
\alias{initialize,traceable-method}
|
6 |
\alias{initialize,traceable-method}
|
| 7 |
\alias{initialize,signature-method}
|
7 |
\alias{initialize,signature-method}
|
| 8 |
\alias{initialize,environment-method}
|
8 |
\alias{initialize,environment-method}
|
| - |
|
9 |
\description{
|
| 9 |
\description{The arguments to function \code{\link{new}} to create an
|
10 |
The arguments to function \code{\link{new}} to create an object from a
|
| 10 |
object from a particular class can be interpreted specially for that
|
11 |
particular class can be interpreted specially for that class, by the
|
| 11 |
class, by the definition of a method for function \code{initialize}
|
12 |
definition of a method for function \code{initialize} for the class.
|
| 12 |
for the class. This documentation describes some existing
|
13 |
This documentation describes some existing methods, and also outlines
|
| 13 |
methods, and also outlines how to write new ones.
|
14 |
how to write new ones.
|
| 14 |
}
|
15 |
}
|
| 15 |
\section{Methods}{\describe{
|
16 |
\section{Methods}{
|
| 16 |
|
17 |
\describe{
|
| - |
|
18 |
\item{.Object = "ANY"}{
|
| 17 |
\item{.Object = "ANY"}{The default method for \code{initialize} takes
|
19 |
The default method for \code{initialize} takes either named or
|
| 18 |
either named or unnamed arguments. Argument names must be the
|
20 |
unnamed arguments. Argument names must be the names of slots in
|
| 19 |
names of slots in this class definition, and the corresponding
|
21 |
this class definition, and the corresponding arguments must be
|
| 20 |
arguments must be valid objects for the slot (that is, have the
|
22 |
valid objects for the slot (that is, have the same class as
|
| 21 |
same class as specified for the slot, or some superclass of that
|
23 |
specified for the slot, or some superclass of that class). If the
|
| 22 |
class). If the object comes from a superclass, it is not coerced
|
24 |
object comes from a superclass, it is not coerced strictly, so
|
| 23 |
strictly, so normally it will retain its current class (specifically,
|
25 |
normally it will retain its current class (specifically,
|
| 24 |
\code{\link{as}(object, Class, strict = FALSE)}).
|
26 |
\code{\link{as}(object, Class, strict = FALSE)}).
|
| 25 |
|
27 |
|
| 26 |
Unnamed arguments must be objects of this class, of one of its
|
28 |
Unnamed arguments must be objects of this class, of one of its
|
| 27 |
superclasses, or one of its subclasses (from the class, from a
|
29 |
superclasses, or one of its subclasses (from the class, from a
|
| 28 |
class this class extends, or from a class that extends this
|
30 |
class this class extends, or from a class that extends this
|
| 29 |
class). If the object is from a superclass, this normally defines
|
31 |
class). If the object is from a superclass, this normally defines
|
| 30 |
some of the slots in the object. If the object is from a
|
32 |
some of the slots in the object. If the object is from a
|
| 31 |
subclass, the new object is that argument, coerced to the current
|
33 |
subclass, the new object is that argument, coerced to the current
|
| 32 |
class.
|
34 |
class.
|
| 33 |
|
35 |
|
| 34 |
Unnamed arguments are processed first, in the order they appear.
|
36 |
Unnamed arguments are processed first, in the order they appear.
|
| 35 |
Then named arguments are processed. Therefore, explicit values
|
37 |
Then named arguments are processed. Therefore, explicit values
|
| 36 |
for slots always override any values inferred from superclass or
|
38 |
for slots always override any values inferred from superclass or
|
| 37 |
subclass arguments.
|
39 |
subclass arguments.
|
| - |
|
40 |
}
|
| - |
|
41 |
|
| - |
|
42 |
\item{.Object = "traceable"}{
|
| - |
|
43 |
Objects of a class that extends \code{traceable} are used to
|
| - |
|
44 |
implement debug tracing (see \link{traceable-class} and
|
| - |
|
45 |
\code{\link[base]{trace}}).
|
| - |
|
46 |
|
| - |
|
47 |
The \code{initialize} method for these classes takes special
|
| - |
|
48 |
arguments \code{def, tracer, exit, at, print}. The first of these
|
| - |
|
49 |
is the object to use as the original definition (e.g., a
|
| - |
|
50 |
function). The others correspond to the arguments to
|
| - |
|
51 |
\code{\link[base]{trace}}.
|
| - |
|
52 |
}
|
| - |
|
53 |
|
| - |
|
54 |
\item{.Object = "environment"}{
|
| - |
|
55 |
The \code{initialize} method for environments takes a named list
|
| - |
|
56 |
of objects to be used to initialize the environment.
|
| - |
|
57 |
}
|
| - |
|
58 |
|
| - |
|
59 |
\item{.Object = "signature"}{
|
| - |
|
60 |
This is a method for internal use only.
|
| - |
|
61 |
It takes an optional \code{functionDef} argument to provide a
|
| - |
|
62 |
generic function with a \code{signature} slot to define the
|
| - |
|
63 |
argument names. See \link{Methods} for details.
|
| - |
|
64 |
}
|
| - |
|
65 |
}
|
| 38 |
}
|
66 |
}
|
| 39 |
|
- |
|
| 40 |
\item{.Object = "traceable"}{ Objects of a class that extends
|
- |
|
| 41 |
\code{traceable} are used to implement debug tracing (see
|
- |
|
| 42 |
\link{traceable-class} and \code{\link[base]{trace}}).
|
- |
|
| 43 |
|
- |
|
| 44 |
The \code{initialize} method for these classes takes special arguments
|
- |
|
| 45 |
\code{def, tracer, exit, at, print}. The first of these is the object
|
- |
|
| 46 |
to use as the original definition (e.g., a function). The others
|
- |
|
| 47 |
correspond to the arguments to \code{\link[base]{trace}}.
|
- |
|
| 48 |
}
|
- |
|
| 49 |
|
- |
|
| 50 |
\item{.Object = "environment"}{The \code{initialize} method for
|
- |
|
| 51 |
environments takes a named list of objects to be used to
|
- |
|
| 52 |
initialize the environment. }
|
- |
|
| 53 |
|
- |
|
| 54 |
\item{.Object = "signature"}{ This is a method for internal use only.
|
- |
|
| 55 |
It takes an optional \code{functionDef} argument to provide a generic
|
- |
|
| 56 |
function with a \code{signature} slot to define the argument names.
|
- |
|
| 57 |
See \link{Methods} for details.
|
- |
|
| 58 |
}
|
- |
|
| 59 |
|
- |
|
| 60 |
|
- |
|
| 61 |
}}
|
- |
|
| 62 |
\section{Writing Initialization Methods}{Initialization methods
|
67 |
\section{Writing Initialization Methods}{
|
| 63 |
provide a general mechanism corresponding to generator functions in
|
68 |
Initialization methods provide a general mechanism corresponding to
|
| 64 |
other languages.
|
69 |
generator functions in other languages.
|
| 65 |
|
70 |
|
| 66 |
The arguments to \code{\link{initialize}} are \code{.Object} and
|
71 |
The arguments to \code{\link{initialize}} are \code{.Object} and
|
| 67 |
\dots. Nearly always, \code{initialize} is called from \code{new},
|
72 |
\dots. Nearly always, \code{initialize} is called from \code{new},
|
| 68 |
not directly. The \code{.Object} argument is then the
|
73 |
not directly. The \code{.Object} argument is then the
|
| 69 |
prototype object from the class.
|
74 |
prototype object from the class.
|
| Line 76... |
Line 81... |
| 76 |
your method definition, in addition to \code{.Object} (always) and
|
81 |
your method definition, in addition to \code{.Object} (always) and
|
| 77 |
\dots (optionally). For example, the method for class
|
82 |
\dots (optionally). For example, the method for class
|
| 78 |
\code{"traceable"} documented above would be created by a call to
|
83 |
\code{"traceable"} documented above would be created by a call to
|
| 79 |
\code{\link{setMethod}} of the form:
|
84 |
\code{\link{setMethod}} of the form:
|
| 80 |
|
85 |
|
| - |
|
86 |
\preformatted{
|
| 81 |
\code{setMethod("initialize", "traceable",}
|
87 |
setMethod("initialize", "traceable",
|
| 82 |
\cr
|
- |
|
| 83 |
\code{ function(.Object, def, tracer, exit, at, print) } \dots
|
88 |
function(.Object, def, tracer, exit, at, print) \dots
|
| 84 |
\cr
|
89 |
)
|
| 85 |
\code{)}
|
90 |
}
|
| 86 |
|
91 |
|
| 87 |
In this example, no other arguments are meaningful, and the resulting
|
92 |
In this example, no other arguments are meaningful, and the resulting
|
| 88 |
method will throw an error if other names are supplied.
|
93 |
method will throw an error if other names are supplied.
|
| 89 |
|
94 |
|
| 90 |
When your new class extends another class, you may want to call the
|
95 |
When your new class extends another class, you may want to call the
|
| 91 |
initialize method for this superclass (either a special method or the
|
96 |
initialize method for this superclass (either a special method or the
|
| 92 |
default). For example, suppose you want to define a method for your
|
97 |
default). For example, suppose you want to define a method for your
|
| 93 |
class, with special argument \code{x}, but you also want users to be
|
98 |
class, with special argument \code{x}, but you also want users to be
|
| 94 |
able to set slots specifically. If you want \code{x} to override the
|
99 |
able to set slots specifically. If you want \code{x} to override the
|
| 95 |
slot information, the beginning of your method definition might look
|
100 |
slot information, the beginning of your method definition might look
|
| 96 |
something like this:
|
101 |
something like this:
|
| 97 |
|
102 |
|
| - |
|
103 |
\preformatted{
|
| 98 |
\code{function(.Object, x, ...) \{}
|
104 |
function(.Object, x, ...) \{
|
| 99 |
\cr
|
- |
|
| 100 |
\code{ Object <- callNextMethod(.Object, ...) }
|
105 |
Object <- callNextMethod(.Object, ...)
|
| 101 |
\cr
|
- |
|
| 102 |
\code{ if(!missing(x)) \{ # do something with x }
|
106 |
if(!missing(x)) \{ # do something with x
|
| - |
|
107 |
}
|
| 103 |
|
108 |
|
| 104 |
You could also choose to have the inherited method override, by first
|
109 |
You could also choose to have the inherited method override, by first
|
| 105 |
interpreting \code{x}, and then calling the next method.
|
110 |
interpreting \code{x}, and then calling the next method.
|
| 106 |
|
111 |
|
| 107 |
}
|
112 |
}
|
| 108 |
\keyword{methods}
|
113 |
\keyword{methods}
|
| 109 |
\keyword{programming}
|
114 |
\keyword{programming}
|