| 42333 |
ripley |
1 |
% File src/library/methods/man/initialize-methods.Rd
|
| 68948 |
ripley |
2 |
% Part of the R package, https://www.R-project.org
|
| 59039 |
ripley |
3 |
% Copyright 1995-2007 R Core Team
|
| 42333 |
ripley |
4 |
% Distributed under GPL 2 or later
|
|
|
5 |
|
| 25351 |
jmc |
6 |
\name{initialize-methods}
|
|
|
7 |
\docType{methods}
|
|
|
8 |
\title{Methods to Initialize New Objects from a Class}
|
| 56186 |
murdoch |
9 |
\alias{initialize-methods}
|
| 25351 |
jmc |
10 |
\alias{initialize,ANY-method}
|
|
|
11 |
\alias{initialize,traceable-method}
|
|
|
12 |
\alias{initialize,signature-method}
|
|
|
13 |
\alias{initialize,environment-method}
|
| 53641 |
jmc |
14 |
\alias{initialize,.environment-method}
|
| 26610 |
ripley |
15 |
\description{
|
|
|
16 |
The arguments to function \code{\link{new}} to create an object from a
|
|
|
17 |
particular class can be interpreted specially for that class, by the
|
|
|
18 |
definition of a method for function \code{initialize} for the class.
|
|
|
19 |
This documentation describes some existing methods, and also outlines
|
|
|
20 |
how to write new ones.
|
| 25351 |
jmc |
21 |
}
|
| 26610 |
ripley |
22 |
\section{Methods}{
|
|
|
23 |
\describe{
|
| 49610 |
hornik |
24 |
\item{\code{signature(.Object = "ANY")}}{
|
| 26610 |
ripley |
25 |
The default method for \code{initialize} takes either named or
|
|
|
26 |
unnamed arguments. Argument names must be the names of slots in
|
|
|
27 |
this class definition, and the corresponding arguments must be
|
|
|
28 |
valid objects for the slot (that is, have the same class as
|
|
|
29 |
specified for the slot, or some superclass of that class). If the
|
|
|
30 |
object comes from a superclass, it is not coerced strictly, so
|
|
|
31 |
normally it will retain its current class (specifically,
|
|
|
32 |
\code{\link{as}(object, Class, strict = FALSE)}).
|
| 25351 |
jmc |
33 |
|
| 26610 |
ripley |
34 |
Unnamed arguments must be objects of this class, of one of its
|
|
|
35 |
superclasses, or one of its subclasses (from the class, from a
|
|
|
36 |
class this class extends, or from a class that extends this
|
|
|
37 |
class). If the object is from a superclass, this normally defines
|
|
|
38 |
some of the slots in the object. If the object is from a
|
|
|
39 |
subclass, the new object is that argument, coerced to the current
|
|
|
40 |
class.
|
| 25351 |
jmc |
41 |
|
| 26610 |
ripley |
42 |
Unnamed arguments are processed first, in the order they appear.
|
|
|
43 |
Then named arguments are processed. Therefore, explicit values
|
|
|
44 |
for slots always override any values inferred from superclass or
|
|
|
45 |
subclass arguments.
|
|
|
46 |
}
|
| 25351 |
jmc |
47 |
|
| 49610 |
hornik |
48 |
\item{\code{signature(.Object = "traceable")}}{
|
| 26610 |
ripley |
49 |
Objects of a class that extends \code{traceable} are used to
|
| 44751 |
maechler |
50 |
implement debug tracing (see class \linkS4class{traceable} and
|
| 30459 |
ripley |
51 |
\code{\link{trace}}).
|
| 25351 |
jmc |
52 |
|
| 26610 |
ripley |
53 |
The \code{initialize} method for these classes takes special
|
|
|
54 |
arguments \code{def, tracer, exit, at, print}. The first of these
|
|
|
55 |
is the object to use as the original definition (e.g., a
|
|
|
56 |
function). The others correspond to the arguments to
|
| 30459 |
ripley |
57 |
\code{\link{trace}}.
|
| 26610 |
ripley |
58 |
}
|
| 25351 |
jmc |
59 |
|
| 53641 |
jmc |
60 |
\item{\code{signature(.Object = "environment")}, \code{signature(.Object = ".environment")}}{
|
| 26610 |
ripley |
61 |
The \code{initialize} method for environments takes a named list
|
| 53641 |
jmc |
62 |
of objects to be used to initialize the environment. Subclasses
|
|
|
63 |
of \code{"environment"} inherit an initialize method through
|
|
|
64 |
\code{".environment"}, which has the additional effect of
|
|
|
65 |
allocating a new environment. If you define your own method for
|
|
|
66 |
such a subclass, be sure either to call the existing method via
|
|
|
67 |
\code{\link{callNextMethod}} or allocate an environment in your
|
|
|
68 |
method, since environments are references and are not duplicated
|
|
|
69 |
automatically.
|
| 26610 |
ripley |
70 |
}
|
| 25351 |
jmc |
71 |
|
| 49610 |
hornik |
72 |
\item{\code{signature(.Object = "signature")}}{
|
| 26610 |
ripley |
73 |
This is a method for internal use only.
|
|
|
74 |
It takes an optional \code{functionDef} argument to provide a
|
|
|
75 |
generic function with a \code{signature} slot to define the
|
| 71366 |
jmc |
76 |
argument names. See \link{Methods_Details} for details.
|
| 26610 |
ripley |
77 |
}
|
|
|
78 |
}
|
|
|
79 |
}
|
|
|
80 |
\section{Writing Initialization Methods}{
|
|
|
81 |
Initialization methods provide a general mechanism corresponding to
|
|
|
82 |
generator functions in other languages.
|
| 25351 |
jmc |
83 |
|
|
|
84 |
The arguments to \code{\link{initialize}} are \code{.Object} and
|
|
|
85 |
\dots. Nearly always, \code{initialize} is called from \code{new},
|
|
|
86 |
not directly. The \code{.Object} argument is then the
|
|
|
87 |
prototype object from the class.
|
|
|
88 |
|
|
|
89 |
Two techniques are often appropriate for \code{initialize} methods:
|
|
|
90 |
special argument names and \code{callNextMethod}.
|
|
|
91 |
|
|
|
92 |
You may want argument names that are more natural to your users than
|
|
|
93 |
the (default) slot names. These will be the formal arguments to
|
|
|
94 |
your method definition, in addition to \code{.Object} (always) and
|
|
|
95 |
\dots (optionally). For example, the method for class
|
|
|
96 |
\code{"traceable"} documented above would be created by a call to
|
|
|
97 |
\code{\link{setMethod}} of the form:
|
|
|
98 |
|
| 55047 |
ripley |
99 |
\preformatted{ setMethod("initialize", "traceable",
|
| 26610 |
ripley |
100 |
function(.Object, def, tracer, exit, at, print) \dots
|
|
|
101 |
)
|
| 55047 |
ripley |
102 |
}
|
| 25351 |
jmc |
103 |
|
| 26610 |
ripley |
104 |
In this example, no other arguments are meaningful, and the resulting
|
|
|
105 |
method will throw an error if other names are supplied.
|
| 25351 |
jmc |
106 |
|
| 26610 |
ripley |
107 |
When your new class extends another class, you may want to call the
|
|
|
108 |
initialize method for this superclass (either a special method or the
|
|
|
109 |
default). For example, suppose you want to define a method for your
|
|
|
110 |
class, with special argument \code{x}, but you also want users to be
|
|
|
111 |
able to set slots specifically. If you want \code{x} to override the
|
|
|
112 |
slot information, the beginning of your method definition might look
|
|
|
113 |
something like this:
|
| 44751 |
maechler |
114 |
|
| 55047 |
ripley |
115 |
\preformatted{ function(.Object, x, ...) \{
|
| 26610 |
ripley |
116 |
Object <- callNextMethod(.Object, ...)
|
|
|
117 |
if(!missing(x)) \{ # do something with x
|
| 55047 |
ripley |
118 |
}
|
| 25351 |
jmc |
119 |
|
| 26610 |
ripley |
120 |
You could also choose to have the inherited method override, by first
|
|
|
121 |
interpreting \code{x}, and then calling the next method.
|
| 25351 |
jmc |
122 |
|
|
|
123 |
}
|
|
|
124 |
\keyword{methods}
|
|
|
125 |
\keyword{programming}
|