[Rd] S4 methods semantics questions

Seth Falcon sfalcon at fhcrc.org
Fri Mar 25 19:36:23 CET 2005


John Chambers <jmc at R-project.org> writes:

> Well, the intent is that defaults are indeed taken from the method, if
> there is a default there, otherwise from the generic.  It looks as if
> there is a bug in the case that the generic has NO default for that
> argument (unless, of course, it's a subtle feature, but not that I can
> think of at the moment).
>
> Your example works as intended if there is a default expression for y
> in the generic:
>
> R> setGeneric("foo",function(x,y=stop("Need y")) standardGeneric("foo"))
> [1] "foo"
> R> setMethod("foo","numeric",function(x,y=2) x+y)
> [1] "foo"
> R> foo(1)
> [1] 3

FWIW, I've encountered the same error that the OP described.  I
certainly don't know if it is a bug, but I can say that I expected it
to work based on the documentation of setMethod.  

I ended up with a different workaround: if you don't need to do
dispatching on the default arguments, you can set

setGeneric("foo", function(x, ...) standardGeneric("foo"))

and then methods with defaults defined in their function do the
expected thing.

Best,

+ seth



More information about the R-devel mailing list