| Line 71... |
Line 71... |
| 71 |
See the section on \dQuote{Implementation} for the internal mechanism
|
71 |
See the section on \dQuote{Implementation} for the internal mechanism
|
| 72 |
used by accessor functions.
|
72 |
used by accessor functions.
|
| 73 |
|
73 |
|
| 74 |
Note that fields are distinct from
|
74 |
Note that fields are distinct from
|
| 75 |
the slots, if any, in the object. Slots are, as always, handled by
|
75 |
the slots, if any, in the object. Slots are, as always, handled by
|
| 76 |
standard \R{} object management. Slots for the class can be included
|
76 |
standard \R{} object management.
|
| - |
|
77 |
It is not generally a good idea to mix slots and fields in the same
|
| 77 |
(as the \code{representation=} argument) in the \dots argument.
|
78 |
class; this confuses the distinction in behavior between reference
|
| - |
|
79 |
classes and regular S4 classes. See the comments in the
|
| - |
|
80 |
\dQuote{Implementation} section.
|
| 78 |
}
|
81 |
}
|
| 79 |
\item{contains}{
|
82 |
\item{contains}{
|
| 80 |
optional vector of superclasses for this class. If a superclass is
|
83 |
optional vector of superclasses for this class. If a superclass is
|
| 81 |
also a reference class, the fields and class-based methods will be inherited.
|
84 |
also a reference class, the fields and class-based methods will be inherited.
|
| 82 |
}
|
85 |
}
|
| Line 182... |
Line 185... |
| 182 |
Normal objects in \R are passed as arguments in function calls consistently with
|
185 |
Normal objects in \R are passed as arguments in function calls consistently with
|
| 183 |
functional programming semantics; that is, changes made to an object
|
186 |
functional programming semantics; that is, changes made to an object
|
| 184 |
passed as an argument are local to the function call. The object that
|
187 |
passed as an argument are local to the function call. The object that
|
| 185 |
supplied the argument is unchanged.
|
188 |
supplied the argument is unchanged.
|
| 186 |
|
189 |
|
| 187 |
The functional model (sometimes called pass-by-value) is
|
190 |
The functional model (sometimes called pass-by-value, although this is
|
| - |
|
191 |
inaccurate for \R) is
|
| 188 |
suitable for many statistical computations and is implicit, for
|
192 |
suitable for many statistical computations and is implicit, for
|
| 189 |
example, in the basic \R software for fitting statistical models.
|
193 |
example, in the basic \R software for fitting statistical models.
|
| 190 |
In some other situations, one would like all the code dealing with an
|
194 |
In some other situations, one would like all the code dealing with an
|
| 191 |
object to see the exact same content, so that changes made in any
|
195 |
object to see the exact same content, so that changes made in any
|
| 192 |
computation would be reflected everywhere.
|
196 |
computation would be reflected everywhere.
|
| Line 196... |
Line 200... |
| 196 |
In addition, commonly used languages, including Java, C++ and many
|
200 |
In addition, commonly used languages, including Java, C++ and many
|
| 197 |
others, support a version of classes and methods assuming reference
|
201 |
others, support a version of classes and methods assuming reference
|
| 198 |
semantics.
|
202 |
semantics.
|
| 199 |
The corresponding programming mechanism
|
203 |
The corresponding programming mechanism
|
| 200 |
is to invoke a method on an object.
|
204 |
is to invoke a method on an object.
|
| 201 |
In the \R syntax that we use \code{"$"} %$
|
205 |
In the \R syntax we use \code{"$"} %$
|
| 202 |
for this operation; one invokes a method,
|
206 |
for this operation; one invokes a method,
|
| 203 |
\code{m1} say, on an object \code{x} by the expression
|
207 |
\code{m1} say, on an object \code{x} by the expression
|
| 204 |
\code{x$m1(...)}. %$
|
208 |
\code{x$m1(...)}. %$
|
| 205 |
|
209 |
|
| 206 |
Methods in this paradigm are associated with the object, or more
|
210 |
Methods in this paradigm are associated with the object, or more
|
| Line 238... |
Line 242... |
| 238 |
Instead, fields and methods for the class can be referred to by name
|
242 |
Instead, fields and methods for the class can be referred to by name
|
| 239 |
in the method definition.
|
243 |
in the method definition.
|
| 240 |
The implementation uses \R environments to make fields and methods
|
244 |
The implementation uses \R environments to make fields and methods
|
| 241 |
available by name.
|
245 |
available by name.
|
| 242 |
Additional special fields allow reference to the complete object and
|
246 |
Additional special fields allow reference to the complete object and
|
| 243 |
to the definition of the class. See the section on \dQuote{Inheritance}.
|
247 |
to the definition of the class. See the section on \dQuote{Writing
|
| - |
|
248 |
Reference Methods}.
|
| 244 |
|
249 |
|
| 245 |
The goal of the software described here is to provide a uniform
|
250 |
The goal of the software described here is to provide a uniform
|
| 246 |
programming style in \R for software dealing with reference classes, whether
|
251 |
programming style in \R for software dealing with reference classes, whether
|
| 247 |
implemented directly in \R or through an interface to one of the OOP
|
252 |
implemented directly in \R or through an interface to one of the OOP
|
| 248 |
languages.
|
253 |
languages.
|
| Line 289... |
Line 294... |
| 289 |
example.
|
294 |
example.
|
| 290 |
The special object \code{.refClassDef} contains the definition of the
|
295 |
The special object \code{.refClassDef} contains the definition of the
|
| 291 |
class of the object.
|
296 |
class of the object.
|
| 292 |
These fields are read-only (it makes no sense to modify these
|
297 |
These fields are read-only (it makes no sense to modify these
|
| 293 |
references), with one exception.
|
298 |
references), with one exception.
|
| 294 |
The \code{.self} field can be modified in the \code{$initialize} %$
|
299 |
In principal, the \code{.self} field can be modified in the \code{$initialize} %$
|
| 295 |
method, because the object is still being created at this stage.
|
300 |
method, because the object is still being created at this stage.
|
| 296 |
(Assignments to this field, as to all fields, need to use the
|
301 |
This is definitely not recommended, unless to set some
|
| 297 |
non-local assignment operator.)
|
- |
|
| 298 |
This is the preferred way to assign S4-style attributes to the object,
|
302 |
non-reference properties of the object defined for this class, which
|
| 299 |
if you need to do so; but remember that these attributes will not
|
- |
|
| 300 |
behave according to reference semantics, unlike fields.
|
303 |
is itself not recommended if it mixes slots and fields.
|
| - |
|
304 |
|
| 301 |
|
305 |
|
| 302 |
The methods available include methods inherited from superclasses, as
|
306 |
The methods available include methods inherited from superclasses, as
|
| 303 |
discussed in the next section.
|
307 |
discussed in the next section.
|
| 304 |
|
308 |
|
| 305 |
Only methods actually used will be included in the environment
|
309 |
Only methods actually used will be included in the environment
|
| 306 |
corresponding to an individual object. If a method requires a
|
310 |
corresponding to an individual object. To declare that a method requires a
|
| 307 |
particular other method, then the first method should include a call
|
311 |
particular other method, the first method should include a call
|
| 308 |
to \code{$usingMethods()} %$
|
312 |
to \code{$usingMethods()} %$
|
| 309 |
with the name of the other method as an argument.
|
313 |
with the name of the other method as an argument.
|
| 310 |
Declaring the methods this way is essential if the other method is used indirectly (e.g., via \code{\link{sapply}()}
|
314 |
Declaring the methods this way is essential if the other method is used indirectly (e.g., via \code{\link{sapply}()}
|
| 311 |
or \code{\link{do.call}()}).
|
315 |
or \code{\link{do.call}()}).
|
| 312 |
If it is called directly, code analysis will find it.
|
316 |
If it is called directly, code analysis will find it.
|
| Line 326... |
Line 330... |
| 326 |
}
|
330 |
}
|
| 327 |
\section{Inheritance}{
|
331 |
\section{Inheritance}{
|
| 328 |
Reference classes inherit from other reference classes by using the
|
332 |
Reference classes inherit from other reference classes by using the
|
| 329 |
standard \R inheritance; that is, by including the superclasses in the
|
333 |
standard \R inheritance; that is, by including the superclasses in the
|
| 330 |
\code{contains=} argument when creating the new class.
|
334 |
\code{contains=} argument when creating the new class.
|
| 331 |
Non-reference classes can also be included in the \code{contains=} argument. The class definition
|
- |
|
| 332 |
mechanism treats reference and non-reference superclasses slightly differently.
|
- |
|
| 333 |
If the contained reference classes themselves have reference
|
- |
|
| 334 |
superclasses, these will be moved ahead of any non-reference
|
- |
|
| 335 |
superclasses in the class definition (otherwise the ordering of
|
- |
|
| 336 |
superclasses may be ambiguous).
|
- |
|
| 337 |
The names of the reference superclasses are in slot
|
335 |
The names of the reference superclasses are in slot
|
| 338 |
\code{refSuperClasses} of the class definition.
|
336 |
\code{refSuperClasses} of the class definition.
|
| - |
|
337 |
Reference classes can inherit from ordinary S4 classes also, but this
|
| - |
|
338 |
is usually a bad idea if it mixes reference fields and non-reference slots.
|
| - |
|
339 |
See the comments in the section on \dQuote{Implementation}.
|
| 339 |
|
340 |
|
| 340 |
Class fields are inherited. A class definition can override a field
|
341 |
Class fields are inherited. A class definition can override a field
|
| 341 |
of the same name in a superclass only if the overriding class is a
|
342 |
of the same name in a superclass only if the overriding class is a
|
| 342 |
subclass of the class of the inherited field. This ensures that a
|
343 |
subclass of the class of the inherited field. This ensures that a
|
| 343 |
valid object in the field remains valid for the superclass as well.
|
344 |
valid object in the field remains valid for the superclass as well.
|
| Line 597... |
Line 598... |
| 597 |
\item{\code{$accessors(...)}}{ %$}{
|
598 |
\item{\code{$accessors(...)}}{ %$}{
|
| 598 |
A number of
|
599 |
A number of
|
| 599 |
systems using the OOP programming paradigm recommend or enforce
|
600 |
systems using the OOP programming paradigm recommend or enforce
|
| 600 |
\emph{getter and setter methods}
|
601 |
\emph{getter and setter methods}
|
| 601 |
corresponding to each field, rather than direct access by name.
|
602 |
corresponding to each field, rather than direct access by name.
|
| 602 |
In the \R version presented here (and fairly often elsewhere
|
603 |
If you like this style and want to extract a field named \code{abc}
|
| 603 |
as well), a field named \code{abc} of an object \code{x} would be
|
- |
|
| 604 |
extracted by \code{x$getAbc()} and assigned by
|
604 |
by \code{x$getAbc()} and assign it by
|
| 605 |
\code{x$setAbc(value)}.
|
605 |
\code{x$setAbc(value)},
|
| 606 |
The \code{$accessors} %$}
|
606 |
the \code{$accessors} %$}
|
| 607 |
method is a convenience function that creates getter and setter methods for the
|
607 |
method is a convenience function that creates such getter and setter methods for the
|
| 608 |
specified fields.
|
608 |
specified fields.
|
| - |
|
609 |
Otherwise there is no reason to use this mechanism. In particular, it
|
| - |
|
610 |
has nothing to do with the general ability to define fields by
|
| - |
|
611 |
functions as described in the section on \dQuote{Reference Objects}.
|
| 609 |
}
|
612 |
}
|
| 610 |
} %% end of \describe
|
613 |
} %% end of \describe
|
| 611 |
} %% end of \section
|
614 |
} %% end of \section
|
| 612 |
|
615 |
|
| 613 |
\section{Implementation}{
|
616 |
\section{Implementation}{
|
| 614 |
Reference classes are implemented as S4 classes with a data part of
|
617 |
Reference classes are implemented as S4 classes with a data part of
|
| 615 |
type \code{"environment"}.
|
618 |
type \code{"environment"}.
|
| 616 |
An object generated from a reference class has this type.
|
- |
|
| 617 |
Fields correspond to named objects in the environment.
|
619 |
Fields correspond to named objects in the environment.
|
| 618 |
A field associated with an accessor function is implemented as an
|
620 |
A field associated with a function is implemented as an
|
| 619 |
\link[=bindenv]{active binding}.
|
621 |
\link[=bindenv]{active binding}.
|
| 620 |
In addition, fields with a specified class are implemented as a
|
622 |
In particular, fields with a specified class are implemented as a
|
| 621 |
special form of active binding to enforce valid assignment to the
|
623 |
special form of active binding to enforce valid assignment to the
|
| 622 |
field.
|
624 |
field.
|
| 623 |
A field, say \code{data}, can be accessed generally by an expression
|
625 |
A field, say \code{data}, can be accessed generally by an expression
|
| 624 |
of the form \code{x$data} %$}
|
626 |
of the form \code{x$data} %$}
|
| 625 |
for any object from the relevant class.
|
627 |
for any object from the relevant class.
|
| Line 637... |
Line 639... |
| 637 |
|
639 |
|
| 638 |
When a method is invoked on an object, the function defining the method is
|
640 |
When a method is invoked on an object, the function defining the method is
|
| 639 |
installed in the object's environment, with the same environment as the
|
641 |
installed in the object's environment, with the same environment as the
|
| 640 |
environment of the function.
|
642 |
environment of the function.
|
| 641 |
|
643 |
|
| - |
|
644 |
Because of the implementation, new reference classes can inherit from
|
| - |
|
645 |
non-reference S4 classes as well as reference classes.
|
| - |
|
646 |
This is usually a bad idea, if the slots from the non-reference
|
| - |
|
647 |
class are thought of as alternatives to fields.
|
| - |
|
648 |
Unless there is some special argument in favor, mixing the functional
|
| - |
|
649 |
and reference paradigms for properties of the same object is
|
| - |
|
650 |
conceptually unclear.
|
| - |
|
651 |
In addition, the initialization method for the class will have to sort
|
| - |
|
652 |
out fields from slots, with a good chance of creating anomalous
|
| - |
|
653 |
behavior for subclasses of this class.
|
| - |
|
654 |
Better in general to define fields analogous to the slots in the S4
|
| - |
|
655 |
class, and to initialize those from an S4 object of that class.
|
| - |
|
656 |
|
| 642 |
}
|
657 |
}
|
| 643 |
|
658 |
|
| 644 |
\section{Inter-System Interfaces}{
|
659 |
\section{Inter-System Interfaces}{
|
| 645 |
A number of
|
660 |
A number of
|
| 646 |
languages use a similar reference-based programming model with classes
|
661 |
languages use a similar reference-based programming model with classes
|
| Line 705... |
Line 720... |
| 705 |
(\code{xx$trace()} or \code{mEdit$trace()} in the example below).
|
720 |
(\code{xx$trace()} or \code{mEdit$trace()} in the example below).
|
| 706 |
Using \code{$trace()} on an object sets up a tracing
|
721 |
Using \code{$trace()} on an object sets up a tracing
|
| 707 |
version for future invocations of the specified method for that
|
722 |
version for future invocations of the specified method for that
|
| 708 |
object.
|
723 |
object.
|
| 709 |
Using \code{$trace()} on the generator for the class sets up a
|
724 |
Using \code{$trace()} on the generator for the class sets up a
|
| 710 |
tracing version for all future objects from that class (and for all
|
725 |
tracing version for all future objects from that class (and sometimes for
|
| 711 |
existing objects that have not yet invoked the traced method, because
|
726 |
existing objects from the class if the method is not declared or
|
| 712 |
reference methods are cached lazily in the object when first invoked).
|
727 |
previously invoked).
|
| 713 |
|
728 |
|
| 714 |
In either case, all the arguments to the standard \code{\link{trace}}
|
729 |
In either case, all the arguments to the standard \code{\link{trace}}
|
| 715 |
function are available, except for \code{signature=} which is
|
730 |
function are available, except for \code{signature=} which is
|
| 716 |
meaningless since reference methods can not be S4 generic functions.
|
731 |
meaningless since reference methods can not be S4 generic functions.
|
| 717 |
This includes the typical style \code{trace(what, browser)} for
|
732 |
This includes the typical style \code{trace(what, browser)} for
|