0

I would like to make == a generic function.

When I run: setGeneric("=="), the definition does not appear to change:

> `==`
function (e1, e2)  .Primitive("==")
> setGeneric("==")
[1] "=="
> `==`
function (e1, e2)  .Primitive("==")

And when I call setgeneric("`==`"), I get the following error:

> setGeneric("`==`")
Error in setGeneric("`==`") : 
  must supply a function skeleton for ‘`==`’, explicitly or via an existing function

I can define the == function with:

`==` <- function(x,y) 42;

And then I can use setGeneric on it. But then I'd have to put the body of the original == there, which seems clunky.

So is there any way to make == be generic in S4?

smci
  • 32,567
  • 20
  • 113
  • 146
Leif Andersen
  • 21,580
  • 20
  • 67
  • 100
  • 3
    The primitive functions should do [method dispatching in the C code](https://github.com/wch/r-source/blob/e0104656589073490591cefb12997178362feb7a/src/main/relop.c#L51). Do you have an example of something that isn't working the way you expect? Can you make a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – MrFlick Jun 19 '15 at 18:13
  • Oh, wow, I was basing that off of the output I got when evaluating ==. I just called == and it did what I want. Here was my code: https://gist.github.com/LeifAndersen/993fa5e320827f8c3654 – Leif Andersen Jun 19 '15 at 18:18
  • So then this matter is settled? No need to call `setGeneric("==")`, right? – MrFlick Jun 19 '15 at 18:20
  • (Should one of us put this in the answer field?) – Leif Andersen Jun 19 '15 at 18:23
  • http://stackoverflow.com/questions/21963291/writing-an-s4-generic-method-with-two-arguments – IRTFM Jun 19 '15 at 19:15

1 Answers1

0

Thanks to MrFlick's response:

It turns out that == is already generic (implement in C). So you don't need to call setGeneric.

Rather, you can just use setMethod.

setMethod("==",
          c(e1="Class1",e2="Class2"),
          funciton(e1,e2) { .... })
Leif Andersen
  • 21,580
  • 20
  • 67
  • 100