| Line 21... |
Line 21... |
| 21 |
\alias{refMethodDef-class}
|
21 |
\alias{refMethodDef-class}
|
| 22 |
\alias{refMethodDefWithTrace-class}
|
22 |
\alias{refMethodDefWithTrace-class}
|
| 23 |
\alias{SuperClassMethod-class}
|
23 |
\alias{SuperClassMethod-class}
|
| 24 |
\alias{show,envRefClass-method}
|
24 |
\alias{show,envRefClass-method}
|
| 25 |
\alias{show,refMethodDef-method}
|
25 |
\alias{show,refMethodDef-method}
|
| - |
|
26 |
\alias{show,externalRefMethod-method}
|
| 26 |
\alias{show,refClassRepresentation-method}
|
27 |
\alias{show,refClassRepresentation-method}
|
| - |
|
28 |
\alias{externalRefMethod}
|
| - |
|
29 |
\alias{externalRefMethod-class}
|
| 27 |
\description{
|
30 |
\description{
|
| 28 |
The software described here supports reference classes whose objects have
|
31 |
The software described here supports reference classes whose objects have
|
| 29 |
fields
|
32 |
fields
|
| 30 |
accessed by reference in the style of \dQuote{OOP} languages such as Java and
|
33 |
accessed by reference in the style of \dQuote{OOP} languages such as Java and
|
| 31 |
C++.
|
34 |
C++.
|
| Line 264... |
Line 267... |
| 264 |
features and restrictions.
|
267 |
features and restrictions.
|
| 265 |
The body of the function can contain calls to any other reference method,
|
268 |
The body of the function can contain calls to any other reference method,
|
| 266 |
including those inherited from other reference classes and may refer
|
269 |
including those inherited from other reference classes and may refer
|
| 267 |
to fields in the object by name.
|
270 |
to fields in the object by name.
|
| 268 |
|
271 |
|
| - |
|
272 |
Alternatively, a method may be an \emph{external} method, a function
|
| - |
|
273 |
whose first argument is \code{.self}.
|
| - |
|
274 |
The body of such methods works like any ordinary function.
|
| - |
|
275 |
The methods are called like other methods (without the \code{.self}
|
| - |
|
276 |
argument, which is supplied internally and always refers to the object
|
| - |
|
277 |
itself).
|
| - |
|
278 |
External methods exist so that reference classes can have superclasses
|
| - |
|
279 |
in other packages; see the Section \dQuote{Inter-Package Superclasses
|
| - |
|
280 |
and External Methods}.
|
| - |
|
281 |
|
| 269 |
Fields may be modified in a method by using the
|
282 |
Fields may be modified in a method by using the
|
| 270 |
non-local assignment operator, \code{<<-}, as in the \code{$edit} and \code{$undo}
|
283 |
non-local assignment operator, \code{<<-}, as in the \code{$edit} and \code{$undo}
|
| 271 |
methods in the example below.
|
284 |
methods in the example below.
|
| 272 |
Note that non-local assignment is required: a local assignment with
|
285 |
Note that non-local assignment is required: a local assignment with
|
| 273 |
the \code{<-} operator just creates a local object in the function
|
286 |
the \code{<-} operator just creates a local object in the function
|
| Line 483... |
Line 496... |
| 483 |
unwise to define a field whose name begins with \code{"."}, since the
|
496 |
unwise to define a field whose name begins with \code{"."}, since the
|
| 484 |
implementation may use such names for special purposes.
|
497 |
implementation may use such names for special purposes.
|
| 485 |
|
498 |
|
| 486 |
}
|
499 |
}
|
| 487 |
|
500 |
|
| - |
|
501 |
\section{Inter-Package Superclasses and External Methods}{
|
| - |
|
502 |
The environment of a method in a reference class is the object itself,
|
| - |
|
503 |
as an environment.
|
| - |
|
504 |
This allows the method to refer directly to fields and other methods,
|
| - |
|
505 |
without using the whole object and the \code{"$"} %$
|
| - |
|
506 |
operator.
|
| - |
|
507 |
The parent of that environment is the namespace of the package in
|
| - |
|
508 |
which the reference class is defined.
|
| - |
|
509 |
Computations in the method have access to all the objects in the
|
| - |
|
510 |
package's namespace, exported or not.
|
| - |
|
511 |
|
| - |
|
512 |
When the class becomes a superclass of a class in another package, the
|
| - |
|
513 |
objects from the subclass must retain the same parent environment
|
| - |
|
514 |
pattern, in order for the inherited methods to work.
|
| - |
|
515 |
As a result, methods for the new class may need another mechanism in
|
| - |
|
516 |
order to refer to non-exported objects in their own package's namespace.
|
| - |
|
517 |
|
| - |
|
518 |
The mechanism is the \emph{external method}. An external method is
|
| - |
|
519 |
written as a function in which the first argument, named \code{.self},
|
| - |
|
520 |
stands for the reference class object.
|
| - |
|
521 |
This function is supplied as the definition for a reference class method.
|
| - |
|
522 |
The method will be called, automatically, with the first argument
|
| - |
|
523 |
being the current object and the other arguments, if any, passed along
|
| - |
|
524 |
from the actual call.
|
| - |
|
525 |
|
| - |
|
526 |
Since an external method is an ordinary function in the source code
|
| - |
|
527 |
for its package, it has access to all the objects in the namespace.
|
| - |
|
528 |
Fields and methods in the reference class must be referred to in the
|
| - |
|
529 |
form \code{.self$name}.%$
|
| - |
|
530 |
|
| - |
|
531 |
If for some reason you do not want to use \code{.self} as the first
|
| - |
|
532 |
argument, a function \code{f()} can be converted explicitly as
|
| - |
|
533 |
\code{externalRefMethod(f)}, which returns an object of class
|
| - |
|
534 |
\code{"externalRefMethod"} that can be supplied as a method for the
|
| - |
|
535 |
class.
|
| - |
|
536 |
The first argument will still correspond to the whole object.
|
| - |
|
537 |
|
| - |
|
538 |
External methods can be supplied for any reference class, but there is no
|
| - |
|
539 |
obvious advantage unless they are needed.
|
| - |
|
540 |
They are more work to write, harder to read and (slightly) slower to
|
| - |
|
541 |
execute.
|
| - |
|
542 |
|
| - |
|
543 |
}
|
| - |
|
544 |
|
| 488 |
\section{Reference Class Generators}{
|
545 |
\section{Reference Class Generators}{
|
| 489 |
The call to \code{setRefClass} defines the specified class and
|
546 |
The call to \code{setRefClass} defines the specified class and
|
| 490 |
returns a \dQuote{generator function} object for that class.
|
547 |
returns a \dQuote{generator function} object for that class.
|
| 491 |
This object has class \code{"refObjectGenerator"}; it inherits
|
548 |
This object has class \code{"refObjectGenerator"}; it inherits
|
| 492 |
from \code{"function"} via \code{"classGeneratorFunction"} and can be
|
549 |
from \code{"function"} via \code{"classGeneratorFunction"} and can be
|
| Line 528... |
Line 585... |
| 528 |
|
585 |
|
| 529 |
If no topic is given or if the topic is not a method name, the
|
586 |
If no topic is given or if the topic is not a method name, the
|
| 530 |
definition of the class is printed.
|
587 |
definition of the class is printed.
|
| 531 |
}
|
588 |
}
|
| 532 |
\item{\code{$methods(...)}}{ %$
|
589 |
\item{\code{$methods(...)}}{ %$
|
| 533 |
With no arguments, returns a list of the reference methods for this
|
590 |
With no arguments, returns the names of the reference methods for this
|
| 534 |
class.
|
591 |
class.
|
| - |
|
592 |
With one character string argument, returns the method of that name.
|
| 535 |
|
593 |
|
| 536 |
Named arguments
|
594 |
Named arguments
|
| 537 |
are method definitions, which will be
|
595 |
are method definitions, which will be
|
| 538 |
installed in the class, as if they had been supplied in the
|
596 |
installed in the class, as if they had been supplied in the
|
| 539 |
\code{methods} argument to \code{setRefClass()}.
|
597 |
\code{methods} argument to \code{setRefClass()}.
|
| 540 |
Supplying methods in this way, rather than in the call to
|
598 |
Supplying methods in this way, rather than in the call to
|
| 541 |
\code{setRefClass()}, is largely for the sake of clearer source code
|
599 |
\code{setRefClass()}, is recommended for the sake of clearer source
|
| - |
|
600 |
code.
|
| 542 |
when many or large methods are being defined.
|
601 |
See the section on \dQuote{Writing Reference Methods} for details.
|
| - |
|
602 |
|
| 543 |
All methods for a class should be defined in the source code that
|
603 |
All methods for a class should be defined in the source code that
|
| 544 |
defines the class, typically as part of a package.
|
604 |
defines the class, typically as part of a package.
|
| 545 |
In particular, methods can not be redefined in a class in an attached
|
605 |
In particular, methods can not be redefined in a class in an attached
|
| 546 |
package with a namespace: The class method checks for a locked
|
606 |
package with a namespace: The class method checks for a locked
|
| 547 |
binding of the class definition.
|
607 |
binding of the class definition.
|