[Rd] "Incompatible methods" for overloaded operator

From: Winston Chang <winstonchang1_at_gmail.com>
Date: Wed, 13 Jun 2012 19:41:16 -0500


I'm trying to overload an operator, and I'm running into a strange problem. It happens when I install and load the package, but not when I simply source() the code.

I'm defining + for two classes. The R code looks like this:
#' @export
#' @method "+" a

`+.a` <- function (x1, x2) {

    message("Running custom + function") }

#' @export
#' @method "+" b
`+.b` <- `+.a`

In some cases I do a+b, and in other cases, I do b+b. I'm told that the +.a and +.b functions must be identical to avoid the error about "Incompatible methods". (In the actual code, the overloaded + function checks the classes of x1 and x2, and then sends them off to other functions.)

This is the NAMESPACE file:
S3method("+",a)
S3method("+",b)

I've put the code up at https://github.com/wch/badadd.

If I just cut and paste the function definitions to my R session, it works fine:
x + y
# Running + function
# NULL
However, if I install and load the package, it gives a warning about incompatible methods, and then seems to fall back to the arithmetic + operator:
library(badadd)
x + y
# [1] 3
# attr(,"class")
# [1] "a"
# Warning message:
# Incompatible methods ("+.a", "+.b") for "+"

Is this expected behavior? And if so, is there a workaround? -Winston

        [[alternative HTML version deleted]]



R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Thu 14 Jun 2012 - 11:14:01 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 21 Jun 2012 - 11:50:30 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive