Say, I am implementing a custom S3 class, called "myclass":
myvec <- 1:5
class(myvec) <- "myclass"
I define Group Ops generics so that the class is preserved after standard operations.
Ops.myclass <- function(e1, e2) {
if(is(e1, "myclass")) {
e1 <- unclass(e1)
structure(NextMethod(), class="myclass")
} else if(is(e2, "myclass")) {
e2 <- unclass(e2)
structure(NextMethod(), class="myclass")
}
}
Now my class is preserved with, for example, addition:
1 + myvec # still has myclass
myvec + 1 # still has myclass
However, when I do this for objects that have their own Group generics, I get into issues:
myvec + Sys.Date()
[1] 19454 19455 19456 19457 19458
attr(,"class")
[1] "myclass"
Warning message:
Incompatible methods ("Ops.myclass", "+.Date") for "+"
Not only does it produce a warning, but in addition the result is different:
unclass(myvec) + Sys.Date()
[1] "2023-04-07" "2023-04-08" "2023-04-09" "2023-04-10" "2023-04-11"
Question: How can I resolve this warning and make this operation return the same result it would return if myvec
didn't have a class?
Basically I want myclass
to have it's own Group Generics, but act subserviently in case of a conflict and give priority to the other class, when in collision.