Skip to content

"Incompatible methods" for overloaded operator

8 messages · Hadley Wickham, Martin Morgan, Kohske Takahashi +1 more

7 days later
#
Hi all,

Any ideas about this?  As far as I can tell it should work - and I
don't understand why it's ok when run outside of a package.

Hadley
On Wed, Jun 13, 2012 at 7:41 PM, Winston Chang <winstonchang1 at gmail.com> wrote:

  
    
#
On 06/20/2012 07:25 PM, Hadley Wickham wrote:
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

   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


Martin

  
    
#
But aren't the methods compatible?  If equality doesn't make a method
compatible what does?
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
#
On 06/20/2012 08:06 PM, Hadley Wickham wrote:
Actually I guess that turns out to be the key (to why they work at the 
command line but not in a package).  At the command line they really 
_are_ the same, e.g., .Internal(inspect("+.a")) has the same  address as 
"+.b". In the package they (e.g., badadd:::"+.a") have different 
addresses, I suppose because S3method() acts on them independently.
rewrite?

  
    
#
In my view the class a and b should inherit same parent, like class c.
And S3methods should be defined for the class c.

Actually, this is not a workaround.
It will go with the more oop-ish design.

kohske

2012/6/21 Winston Chang <winstonchang1 at gmail.com>:

  
    
4 days later