An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-devel/attachments/20120613/9a6e2f5b/attachment.pl>
"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:
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
Assistant Professor / Dobelman Family Junior Chair Department of Statistics / Rice University http://had.co.nz/
On 06/20/2012 07:25 PM, Hadley Wickham wrote:
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.
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
Hadley On Wed, Jun 13, 2012 at 7:41 PM, Winston Chang<winstonchang1 at gmail.com> wrote:
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
Computational Biology / Fred Hutchinson Cancer Research Center 1100 Fairview Ave. N. PO Box 19024 Seattle, WA 98109 Location: Arnold Building M1 B861 Phone: (206) 667-2793
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/
On 06/20/2012 08:06 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
But aren't the methods compatible? If equality doesn't make a method compatible what does?
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.
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 :/
rewrite?
Hadley
Computational Biology / Fred Hutchinson Cancer Research Center 1100 Fairview Ave. N. PO Box 19024 Seattle, WA 98109 Location: Arnold Building M1 B861 Phone: (206) 667-2793
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-devel/attachments/20120620/ec6b5caa/attachment.pl>
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>:
On Wed, Jun 20, 2012 at 10:49 PM, Martin Morgan <mtmorgan at fhcrc.org> wrote:
On 06/20/2012 08:06 PM, Hadley Wickham wrote:
But aren't the methods compatible? ?If equality doesn't make a method compatible what does?
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.
Thanks for the investigation. I wonder if there's some sort of workaround.
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 :/
rewrite?
Yes, the original question is the result of some issues that came up in ggplot2 development... -Winston ? ? ? ?[[alternative HTML version deleted]]
______________________________________________ R-devel at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Kohske Takahashi <takahashi.kohske at gmail.com> Assistant Professor, Research Center for Advanced Science and Technology, The University of ?Tokyo, Japan. http://www.fennel.rcast.u-tokyo.ac.jp/profilee_ktakahashi.html
4 days later
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-devel/attachments/20120625/10e5f528/attachment.pl>