You can do this by defining +.a and +.b as the same function. For instance:
a <- "a" class(a) <- "a" b <- "b" class(b) <- "b" `+.a` <- function(e1, e2){ paste(class(e1), "+", class(e2)) } `+.b` <- `+.a` a+a
If you define Ops.a and Ops.b , it will also define an operation for other operators that can be accessed via .Generic in the function:
Update : another thing I discovered while playing with this. If you put this in a package, you will get the warning "non-numeric argument" and the warning "incompatible operators". This is due to the fact that R works with only a few operators, if they are exactly the same object, with the same address in memory, but somehow in the building and loading the package, both functions lose this exact identification. (You can verify this using pryr::address() )
One thing I discovered is to explicitly register S3 methods when loading the package. For example, this will go into your package:
# Shows the classes of the two objects that are passed in showclasses <- function(e1, e2) { paste(class(e1), "+", class(e2)) } .onLoad <- function(libname, pkgname) { registerS3method("+", "a", showclasses) registerS3method("+", "b", showclasses) }
In this case, two methods point to the same object in memory, and it works (although it is a bit hacked).
source share