calling setGeneric() twice (don't; documentation comments)
On Tue, 2010-01-19 at 12:55 -0800, Seth Falcon wrote:
I would expect setGeneric to create a new generic function and nuke/mask methods associated with the generic that it replaces.
I tried a test in R 2.7.1, and that is the behavior. I think it would
be worthwhile to document it in ?setGeneric.
Also, ?setGeneric advocates first defining a regular function (e.g.,
bar) and then doing a simple setGeneric("bar"). I think the advice for
package developers is different, so perhaps some changes there would be
a good idea too.
I thought I was defining setGeneric twice for a few functions, and thus
that it did work OK. It turns out I have no duplicate definitions.
Here's the test:
setClass("A", representation(z="ANY"))
[1] "A"
setClass("B", representation(y="ANY"))
[1] "B"
setGeneric("foo", function(x) standardGeneric("foo"))
[1] "foo"
setMethod("foo", signature(x="A"), function(x) return("foo for A"))
[1] "foo"
a=new("A")
b=new("B")
foo(a)
[1] "foo for A"
foo(b)
Error in function (classes, fdef, mtable) : unable to find an inherited method for function "foo", for signature "B"
setGeneric("foo", function(x) standardGeneric("foo"))
[1] "foo"
setMethod("foo", signature(x="B"), function(x) return("foo for B"))
[1] "foo"
setGeneric("foo", function(x) standardGeneric("foo"))
[1] "foo"
setMethod("foo", signature(x="B"), function(x) return("foo for B"))
[1] "foo"
foo(a)
# here's where the disappearance of the prior setMethod shows Error in function (classes, fdef, mtable) : unable to find an inherited method for function "foo", for signature "A"
foo(b)
[1] "foo for B" So I guess I am going to pull the setGeneric's out. Ross