From sfalcon at fhcrc.org  Fri Mar 25 19:36:23 2005
From: sfalcon at fhcrc.org (Seth Falcon)
Date: Fri Mar 25 19:36:33 2005
Subject: [Rd] S4 methods semantics questions
In-Reply-To: <42442D8F.9040807@R-project.org> (John Chambers's message of
	"Fri, 25 Mar 2005 10:26:07 -0500")
References: <422af62516432df52f025c8bf6d3e48c@stat.harvard.edu>
	<42442D8F.9040807@R-project.org>
Message-ID: <m2ll8bzi6w.fsf@macaroni.local>

John Chambers <jmc@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


[Rd] S4 methods semantics questions
Seth Falcon