I'm relatively new at OOP and need advice: What is the best way to overload the Arithmetic generic operators in reference classes in R?
For example, suppose I define
bar <- setRefClass( "foo", fields=list(a="numeric", b="numeric" ))
If I try the obvious thing:
> bar$new(a=3,b=1) + bar$new(a=1,b=3)
Error in bar$new(a = 3, b = 1) + bar$new(a = 1, b = 3) :
non-numeric argument to binary operator
What is the recommended way to do something like (a+a) + (b+b)?
You can take advantage of the fact that reference classes are S4 + environments, and define an S4 method:
It's similarly easy to define a set of methods for a group generic: