[Rd] class(<matrix>) |--> c("matrix", "arrary") [was "head.matrix ..."]

Martin Maechler m@ech|er @end|ng |rom @t@t@m@th@ethz@ch
Sun Nov 10 09:36:56 CET 2019

>>>>> Gabriel Becker 
>>>>>     on Sat, 2 Nov 2019 12:37:08 -0700 writes:

    > I agree that we can be careful and narrow and still see a
    > nice improvement in behavior. While Herve's point is valid
    > and I understand his frustration, I think staying within
    > the matrix vs c(matrix, array) space is the right scope
    > for this work in terms of fiddling with inheritance.


> > Also, we seem to have a rule that inherits(x, c)  iff  c %in% class(x),
> good point, and that's why my usage of  inherits(.,.) was not
> quite to the point.  [OTOH, it was to the point, as indeed from
>       the ?class / ?inherits docu, S3 method dispatch and inherits
>       must be consistent ]
>     > which would break -- unless we change class(x) to return the whole
> set of inherited classes, which I sense that we'd rather not do....


> Note again that both "matrix" and "array" are special [see ?class] as
> being of  __implicit class__  and I am considering that this
> implicit class behavior for these two should be slightly
> changed ....
> And indeed I think you are right on spot and this would mean
> that indeed the implicit class
> "matrix" should rather become c("matrix", "array").

I've made up my mind (and not been contradicted by my fellow R
corers) to try go there for  R 4.0.0   next April.

I've found the few places in base R that needed a change (to
pass 'make check-all' in the R sources) and found that indeed a
overzealous check in 'Matrix' needed also a change (a place
where the checking code assume  class(<matrix>) |--> "matrix" ).

There are certainly many more package (codes and checks) that
need adaption .. i.e., should be changed rather *before* the
above change is activated in R-devel (and then will affect all CRAN
and Bioconductor checks.)

To this end, I've published an  'R Blog' yesterday,


which translates to


notably mentioning why using  class(x) == "...."  (or '!=')  or
switch(class(.) ...)  is quite unsafe and hence bad and you
should very often not replace  class(x)  by  class(x)[1]  but
really use the "only truly correct" ;-)

     inherits(x,  "...")
     is(x,  "....")   # if you're advanced/brave enough (:-) to
     	    	      # use formal classes (S4)
Martin Maechler
ETH Zurich and R Core Team

More information about the R-devel mailing list