The R Project SVN R

Rev

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

Rev Author Line No. Line
25351 jmc 1
\name{initialize-methods}
2
\docType{methods}
3
\title{Methods to Initialize New Objects from a Class}
4
\alias{initialize-methods}
5
\alias{initialize,ANY-method}
6
\alias{initialize,traceable-method}
7
\alias{initialize,signature-method}
8
\alias{initialize,environment-method}
26610 ripley 9
\description{
10
  The arguments to function \code{\link{new}} to create an object from a
11
  particular class can be interpreted specially for that class, by the
12
  definition of a method for function \code{initialize} for the class.
13
  This documentation describes some existing methods, and also outlines
14
  how to write new ones.
25351 jmc 15
}
26610 ripley 16
\section{Methods}{
17
  \describe{
18
    \item{.Object = "ANY"}{
19
      The default method for \code{initialize} takes either named or
20
      unnamed arguments.  Argument names must be the names of slots in
21
      this class definition, and the corresponding arguments must be
22
      valid objects for the slot (that is, have the same class as
23
      specified for the slot, or some superclass of that class).  If the
24
      object comes from a superclass, it is not coerced strictly, so
25
      normally it will retain its current class (specifically,
26
      \code{\link{as}(object, Class, strict = FALSE)}).
25351 jmc 27
 
26610 ripley 28
      Unnamed arguments must be objects of this class, of one of its
29
      superclasses, or one of its subclasses (from the class, from a
30
      class this class extends, or from a class that extends this
31
      class). If the object is from a superclass, this normally defines
32
      some of the slots in the object.  If the object is from a
33
      subclass, the new object is that argument, coerced to the current
34
      class.
25351 jmc 35
 
26610 ripley 36
      Unnamed arguments are processed first, in the order they appear.
37
      Then named arguments are processed.  Therefore, explicit values
38
      for slots always override any values inferred from superclass or
39
      subclass arguments.
40
    }
25351 jmc 41
 
26610 ripley 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}}).
25351 jmc 46
 
26610 ripley 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
    }
25351 jmc 53
 
26610 ripley 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
    }
25351 jmc 58
 
26610 ripley 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
  }
66
}
67
\section{Writing Initialization Methods}{
68
  Initialization methods provide a general mechanism corresponding to
69
  generator functions in other languages.
25351 jmc 70
 
71
  The arguments to \code{\link{initialize}} are \code{.Object} and
72
  \dots. Nearly always, \code{initialize} is called from \code{new},
73
  not directly.  The \code{.Object} argument is then the
74
  prototype object from the class.
75
 
76
  Two techniques are often appropriate for \code{initialize} methods:
77
  special argument names and \code{callNextMethod}.
78
 
79
  You may want argument names that are more natural to your users than
80
  the (default) slot names.  These will be the formal arguments to
81
  your method definition, in addition to \code{.Object} (always) and
82
  \dots (optionally).  For example, the method for class
83
  \code{"traceable"} documented above would be created by a call to
84
  \code{\link{setMethod}} of the form:
85
 
26610 ripley 86
  \preformatted{
87
    setMethod("initialize", "traceable",
88
      function(.Object, def, tracer, exit, at, print) \dots
89
    )
90
  }
25351 jmc 91
 
26610 ripley 92
  In this example, no other arguments are meaningful, and the resulting
93
  method will throw an error if other names are supplied.
25351 jmc 94
 
26610 ripley 95
  When your new class extends another class, you may want to call the
96
  initialize method for this superclass (either a special method or the
97
  default).  For example, suppose you want to define a method for your
98
  class, with special argument \code{x}, but you also want users to be
99
  able to set slots specifically.  If you want \code{x} to override the
100
  slot information, the beginning of your method definition might look
101
  something like this:
102
 
103
  \preformatted{
104
    function(.Object, x, ...) \{
105
      Object <- callNextMethod(.Object, ...)
106
      if(!missing(x)) \{ # do something with x
107
  }
25351 jmc 108
 
26610 ripley 109
  You could also choose to have the inherited method override, by first
110
  interpreting \code{x}, and then calling the next method.
25351 jmc 111
 
112
}
113
\keyword{methods}
114
\keyword{programming}