Question

I am trying to define the "c" method for an object I created.

something like

setMethod("c", 
          signature(...), 
          definition=function (...) {
            myObject = list(...)[[1]]
            myObject@mySlot=lapply(list(...), FUN = function(x) slot(x, "mySlot"))
            return(myObject)
         }
)

The problem is that I cannot define the class of ... so that the dispatching is done properly. Any idea?

Was it helpful?

Solution

Elaborating on the comment of @hadley the signature should be for your class, and the definition should follow getGeneric. Hence

> getGeneric("c")
standardGeneric for "c" defined from package "base"

function (x, ..., recursive = FALSE)
standardGeneric("c", .Primitive("c"))
<environment: 0x4956ab8>
Methods may be defined for arguments: x, recursive
Use  showMethods("c")  for currently available ones.

so

setClass("A", representation(x="numeric"))
setMethod("c", "A", function(x, ..., recursive=FALSE) {
  "here I am"
})

and

> c(new("A"), new("A"))
[1] "here I am"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top