我有一个希望%*%
定义方法的 R6 类。我在另一个问题中看到了如何使用新的 S4 方法来实现这一点。但是,我尝试过这种方法,但当我尝试%*%
为我的 R6 类创建 S4 方法时它失败了。例如:
library(R6)
setOldClass(c("TestR6", "R6"))
TestR6 <- R6Class("TestR6", public = list(.x = "numeric"))
setMethod("%*%", c(x = "TestR6", y = "TestR6"),
function(x, y){
print('the matmult operation')
}
)
x <- TestR6$new()
y <- TestR6$new()
x %*% y
x %*% y 中的错误:需要数字/复数矩阵/向量参数
然而,如果我创建该foo
方法,它仍然有效。
setGeneric("foo", signature = "x",
def = function(x) standardGeneric("foo")
)
setMethod("foo", c(x = "R6"),
definition = function(x) {
"I'm the method for `R6`"
})
try(foo(x = TestR6$new()))
[1] "I'm the method for `R6`"
为什么它不适用于该%*%
方法?