[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

7.1 Adding new generics

When creating a new generic function, bear in mind that its argument list will be the maximal set of arguments for methods, including those written elsewhere years later. So choosing a good set of arguments may well be an important design issue, and there need to be good arguments not to include a argument.

If a argument is supplied, some thought should be given to its position in the argument sequence. Arguments which follow must be named in calls to the function, and they must be named in full (partial matching is suppressed after ). Formal arguments before can be partially matched, and so may `swallow' actual arguments intended for . Although it is commonplace to make the argument the last one, that is not always the right choice.

Sometimes package writers want to make generic a function in the base package, and request a change in R. This may be justifiable, but making a function generic with the old definition as the default method does have a small performance cost. It is never necessary, as a package can take over a function in the base package and make it generic by something like

 
foo <- function(object, ...) UseMethod("foo")
foo.default <- function(object, ...) base::foo(object)

Earlier versions of this manual suggested assigning foo.default <- base::foo. This is not a good idea, as it captures the base function at the time of installation and it might be changed as R is patched or updated.

The same idea can be applied for functions in other packages with namespaces.


This document was generated by root on April, 26 2012 using texi2html 1.76.