methods
package in the r-devel codemethods
, has been added to the source
repository for the 1.4 version of R.
The contents provide an implementation of formal classes and methods,
treating the description in the book Programming with Data
(aka the green book). The following notes introduce a
few topics for discussion regarding R development. (The source of the
package replaces, and updates, the Omegahat module
R/Classes
.)
The code is currently being kept in a package, in the main CVS tree so it's guaranteed to be shipped with the corresponding development version of R.
This may be a temporary situation, but it has the advantage that one can detach the package, if it turns out to break something else (e.g., see the next list item). Discussions so far suggest that in the long run at least part of the code will need to be pretty tightly bound to the evaluator for efficiency and to work well with some other new developments such as threading.
The eventual choice may be to incorporate all of the package into the base. We should discuss this as things move along.
class
, etc.
You will see when attaching methods
that it
overrides class
. This is a potential source of
non-compatibility with old-style class code. Details are in a
separate note.
The @
operator has been added for accessing
slots--this is pretty trivial, but as it's implemented as a
primitive, it is now in the base.
I would also like to add a limited use of
=
for assignments: only to be allowed for
top-level assignments or for elements of a braced list. The
limitation removes any chance of the notorious
if(x = 0) /* do something important */
C bug, but still allows the PwD examples to be run. (It's also
useful, I think, in bringing in new users who have programmed in
C, Java, even Fortran.)
We need eventually to decide how to deal with these, if it turns out we want to make S4-style methods the standard. The current implementation is fairly back-compatible, in the sense for example that one can treat matrices as a formal class and usually find that existing matrices conform. (Because R slots are implemented as attributes.)
Some points for discussion are collected in a separate page.
The following are some of the main known differences from the description in Programming with Data.
?f(x, y)
or
class?myClass
). Suggestions would be welcome
on how to do this. Also, decisions about the future of
documentation in R, generally, will make a difference on
how we should deal with special documentation for classes
and methods.
showMethods
) that work across
multiple generic functions; validity methods for classes;
version management (which has never developed much for
S-Plus either).