Make R matrix multiplication `%*%` dispatch on S3 objects

Viewed 48

I have an S3 class that has methods for most of the Math and Ops generics, and I would like for it to also work with %*%. Why doesn't this work?

setOldClass("foo")

setMethod("%*%", signature(x = "foo", y = "foo"),
          function(x, y) { "foo-foo" })

x <- structure(list(), class = "foo")
y <- structure(list(), class = "foo")
x %*% y
#> Error in x %*% y: requires numeric/complex matrix/vector arguments
0 Answers
Related