But getting this error
Error in setGeneric(" ", function(x, y) standardGeneric(" ")) :
' ' dispatches internally; methods can be defined, but the generic function is implicit, and cannot be changed.
Execution halted
after running the below code
> setClass("string", representation(
data = "character"))
>
> setGeneric(" ", function(x, y) standardGeneric(" "))
setMethod(" ", "string", "string", function(x, y) {
new("string", data = paste0(x@data, y@data)) })
CodePudding user response:
I do not know much of S4 classes, but a quick help page shows that:
setClass("string", representation(data = "character"))
setGeneric(" .string", function(x, y) new("string", data = paste0(x@data, y@data)))
x <- new("string", data = "stack")
y <- new("string", data = "overflow")
x y
An object of class "string"
Slot "data":
[1] "stackoverflow"
CodePudding user response:
" "
already exists as a generic so you can't use setGeneric(" "
I think (I don't master S4). Here is a way which works:
setClass(
"string",
slots = c(data = "character")
)
setMethod(
" ",
signature(e1 = "string", e2 = "string"),
function(e1, e2) {
new("string", data = paste0(e1@data, e2@data))
}
)
x <- new("string", data = "stack")
y <- new("string", data = "overflow")
x y