[Rd] "Incompatible methods" for overloaded operator

Hadley Wickham hadley at rice.edu
Thu Jun 21 05:06:47 CEST 2012


> from ?groupGeneric under 'Ops' (of which "+" is one)
>
>          used.  If different methods are found, there is a warning
>          about 'incompatible methods': in that case or if no method is
>          found for either argument the internal method is used.
>
> which doesn't really explain why it works at the command line but not in a
> package. S4 methods can be defined on both arguments, so

But aren't the methods compatible?  If equality doesn't make a method
compatible what does?

>  setClass("A", contains="numeric")
>  setClass("B", contains="numeric")
>
>  setMethod("+", c("A", "A"), function(e1, e2) message("A + A"))
>  setMethod("+", c("A", "B"), function(e1, e2) message("A + B"))
>  setMethod("+", c("B", "A"), function(e1, e2) message("B + A"))
>  setMethod("+", c("B", "B"), function(e1, e2) message("B + B"))
>
> with
>
>  exportClasses("A", "B")
>  exportMethods("+")
>
> and then
>
>> new("A") + new("B")
> A + B
> NULL

But unfortunately that doesn't work for S3 classes (even with
setOldClass) so it doesn't help much unless we want to rewrite
everything in S4 :/

Hadley

-- 
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/



More information about the R-devel mailing list