> > "These operators are also implicit S4 generics, but as > > primitives, S4 methods will be dispatched only on S4 > > objects ?x?."> Yes, exactly, very well found, Georgi!I'm sorry Martin, but I don't understand your point here. I'm assuming that you want the (S3) matrix, x, to be converted to an (S4) Matrix. However, this is not a question of method dispatch, as such. But rather a question of type conversion (integer to numeric to complex, etc). Specifically, can/should automatic type conversion, convert an S3 data type to an S4 data type, even where user-defined data types are involved?
Abby, my answer was too concise. The thrust is that even if you define a method for "[<-" with signature x="matrix" and value ="Matrix", for example, it will never be used since "matrix" is S3. If instead x="someS4class" then the S4 method will be invoked. There may be cases when changing the class of the left-hand side make sense (such as one subclass of "Matrix" to another) but certainly not for the base R vector classes. Georgi Boshnakov -----Original Message----- From: Abby Spurdle <spurdle.a at gmail.com> Sent: 11 September 2020 03:03 To: Martin Maechler <maechler at stat.math.ethz.ch> Cc: Georgi Boshnakov <georgi.boshnakov at manchester.ac.uk>; r-devel at r-project.org Subject: Re: [Rd] more Matrix weirdness> > "These operators are also implicit S4 generics, but as > > primitives, S4 methods will be dispatched only on S4 > > objects ?x?."> Yes, exactly, very well found, Georgi!I'm sorry Martin, but I don't understand your point here. I'm assuming that you want the (S3) matrix, x, to be converted to an (S4) Matrix. However, this is not a question of method dispatch, as such. But rather a question of type conversion (integer to numeric to complex, etc). Specifically, can/should automatic type conversion, convert an S3 data type to an S4 data type, even where user-defined data types are involved?
> There may be cases when changing the class of the left-hand side make sense (such as one subclass of "Matrix" to another) but certainly not for the base R vector classes.I'm not sure what you mean by "not for the base R vector classes". Historically, the simpler class (or mode) gets coerced to the more complex class (or mode). x <- y <- 1:10 y [1] <- 1 (class (x) == class (y) ) #FALSE Also, I note the behavior of multiplication of a matrix with a Matrix. library (Matrix) m <- matrix (1:16, 4, 4) M <- Matrix (1:16, 4, 4) as.vector (class (m * M) ) #dgeMatrix as.vector (class (M * m) ) #dgeMatrix as.vector (class (m %*% M) ) #dgeMatrix as.vector (class (M %*% m) ) #dgeMatrix So, here also, the output is a Matrix, regardless of the type of multiplication, or the order of the operands. But the following surprised me: k <- m mode (k) <- "complex" k %*% M