You should consider writing an Ops.myclss
method. It gives you the ability to handle all group methods in a single function.
See ?base::Ops
, and methods(Ops)
for example implementations. There's also a bit of information in Section 5.6 - Group Methods of R Language Definition.
Here's an example Ops.myclass
that is a slightly modified version of Ops.Date
:
Ops.myclass <-
function (e1, e2)
{
if (nargs() == 1)
stop(gettextf("unary %s not defined for \"myclass\" objects",
.Generic), domain = NA)
boolean <- switch(.Generic, `<` = , `>` = , `==` = , `!=` = ,
`<=` = , `>=` = TRUE, FALSE)
if (!boolean)
stop(gettextf("%s not defined for \"myclass\" objects",
.Generic), domain = NA)
if (inherits(e1, "myclass"))
e1 <- e1$B
if (inherits(e2, "myclass"))
e2 <- e2$B
NextMethod(.Generic)
}
Note that comparison will work whether or not e1
or e2
are myclass
objects. And it will also work for any classes that extend myclass
but do not define their own Ops
method (though the error message would be confusing).