Skip to content
Prev 51824 / 63424 Next

S3 dispatch for S4 subclasses only works if variable "extends" is accessible from global environment

Scenario: An S3 method is declared for an S4 base class but called for 
an instance of a derived class.

Steps to reproduce:

 > Rscript -e "test <- function(x) UseMethod('test', x); test.Matrix <- 
function(x) 'Hi'; MatrixDispatchTest::test(Matrix::Matrix())"
Error in UseMethod("test", x) :
   no applicable method for 'test' applied to an object of class "lsyMatrix"
Calls: <Anonymous>
1: MatrixDispatchTest::test(Matrix::Matrix())

 > Rscript -e "extends <- 42; test <- function(x) UseMethod('test', x); 
test.Matrix <- function(x) 'Hi'; MatrixDispatchTest::test(Matrix::Matrix())"
[1] "Hi"

To me, it looks like a sanity check in line 655 of src/main/attrib.c is 
making wrong assumptions, but there might be other reasons. 
(https://github.com/wch/r-source/blob/780021752eb83a71e2198019acf069ba8741103b/src/main/attrib.c#L655-L656)

Same behavior in R 3.2.4, R 3.2.5 and R-devel r70420.


Best regards

Kirill

Thread (13 messages)

Kirill Müller S3 dispatch for S4 subclasses only works if variable "extends" is accessible from global environment Apr 18 Kirill Müller S3 dispatch for S4 subclasses only works if variable "extends" is accessible from global environment Apr 18 Michael Lawrence S3 dispatch for S4 subclasses only works if variable "extends" is accessible from global environment Apr 18 Kirill Müller S3 dispatch for S4 subclasses only works if variable "extends" is accessible from global environment Apr 18 Michael Lawrence S3 dispatch for S4 subclasses only works if variable "extends" is accessible from global environment Apr 19 Gabriel Becker S3 dispatch for S4 subclasses only works if variable "extends" is accessible from global environment Apr 19 Michael Lawrence S3 dispatch for S4 subclasses only works if variable "extends" is accessible from global environment Apr 19 Gabriel Becker S3 dispatch for S4 subclasses only works if variable "extends" is accessible from global environment Apr 19 Hadley Wickham S3 dispatch for S4 subclasses only works if variable "extends" is accessible from global environment Apr 19 Henrik Bengtsson S3 dispatch for S4 subclasses only works if variable "extends" is accessible from global environment Apr 19 Dirk Eddelbuettel S3 dispatch for S4 subclasses only works if variable "extends" is accessible from global environment Apr 19 Michael Lawrence S3 dispatch for S4 subclasses only works if variable "extends" is accessible from global environment Apr 19 Martin Maechler S3 dispatch for S4 subclasses only works if variable "extends" is accessible from global environment Apr 20