Skip to content
Prev 37100 / 63424 Next

how to define method for "+" function in a new class

On 07/07/2010 08:09 AM, james.foadi at diamond.ac.uk wrote:
If I
standardGeneric for "+" defined from package "base"
  belonging to group(s): Arith

function (e1, e2)
standardGeneric("+", .Primitive("+"))
<environment: 0xb432f8>
Methods may be defined for arguments: e1, e2
Use  showMethods("+")  for currently available ones.

I see that the generic is defined to take two arguments e1 and e2. So

setMethod("+", c("Molecule", "Molecule"), function(e1, e2) {
    ## ...
})

but actually here it might often pay to discover ?GroupGenericFunctions
and end up with something like


setClass("A", representation=representation(x="numeric"))

setMethod("Arith", c("A", "A"), function(e1, e2) {
   new(class(e1), x=callGeneric(e1=e1 at x, e2=e2 at x))
})

and then
An object of class "A"
Slot "x":
[1] 6 6 6 6 6

but also
An object of class "A"
Slot "x":
[1] 5 8 9 8 5

Martin