[R] Ops method does not dispatch on either of two classes

Jens Oehlschlägel oehl_list at gmx.de
Thu Dec 31 15:12:09 CET 2009


Thanks Brian,

> This is as documented on the help page for Ops (the page in base, not 
> the one in Methods) which is linked from ?"|".  For background you 
> should also read the reference on that help page.

Unfortunately I have no access to that book.

> You are wrong in asserting that the internal method is 'for logicals': 
> please do study the help page.  It covers e.g. integer vectors which 
> is what I suspect you have here (assuming this is something to do with 
> package 'bit', unmentioned).

Yes, both "bit" and "bitwhich" are integer with an S3 class attribute (bitwhich sometimes is logical instead).

I am lost. What do I need to do to get "|.a" dispatched if calling 
a | b
where "a" and "b" are objects from S3 classes "a" and "b" that both have methods defined for "|"
?

In the R Language definition I find
"If they do not suggest a single method then the default method is used."
Does this mean it is not possible to write Ops methods for classes "a" and "b" such that "|.a" is called in 
a | b
?

I don't see how I can get any hook into the dispatch mechanism, my methods are always bypassed if the classes of e1 and e2 differ (simple example below).
Best wishes for 2010

Jens Oehlschlägel



> ca <- function(x){
+   x <- as.integer(x)
+   oldClass(x) <- "a"
+   x
+ }
> cb <- function(x){
+   x <- as.integer(x)
+   oldClass(x) <- "b"
+   x
+ }
> 
> a <- ca(1)
> b <- cb(1)
> 
> Ops.a <-
+ function(e1, e2){
+   cat("here Ops.a \n")
+   NULL
+ }
> 
> Ops.b <-
+ function(e1, e2){
+   cat("here Ops.a \n")
+   NULL
+ }
> 
> # OK, "Ops.a" dispatched
> a | a
here |.a 
NULL
> 
> # BUT both, "Ops.a" and "Ops.b" bypassed
> a | b
[1] TRUE
Warning message:
Incompatible methods ("|.a", "|.b") for "|" 
> 
> 
> "|.a" <- function(e1, e2){
+ cat("here |.a \n")
+ NULL
+ }
> 
> "|.b" <- function(e1, e2){
+ cat("here |.b \n")
+ NULL
+ }
> 
> # OK, "|.a" dispatched
> a | a
here |.a 
NULL
> 
> # BUT both, "|.a" and "|.b" bypassed
> a | b
[1] TRUE
Warning message:
Incompatible methods ("|.a", "|.b") for "|" 




More information about the R-help mailing list