Re: [Rd] inconsistencies between ?class and ?UseMethod

From: <>
Date: Tue, 04 Dec 2012 21:40:07 -0800

Hervé Pagès <> writes:

> Hi,
> The 2 man pages give inconsistent description of class():
> Found in ?class:
> If the object does not have a class attribute, it has an implicit
> class, ‘"matrix"’, ‘"array"’ or the result of ‘mode(x)’ (except
> that integer vectors have implicit class ‘"integer"’).
> Found in ?UseMethod:
> Matrices and arrays have class ‘"matrix"’ or‘"array"’ followed
> by the class of the underlying vector.
> Most vectors have class the result of ‘mode(x)’, except that
> integer vectors have class ‘c("integer", "numeric")’ and real
> vectors have class ‘c("double", "numeric")’.
> So according to ?UseMethod, class(matrix(1:4)) should be
> c("matrix", "integer", "numeric"), which is of course not the case:
> > class(matrix(1:4))
> [1] "matrix"
> I wonder if this was ever true, and, if so, when and why it has changed.

It still is in the sense that UseMethod and NextMethod dispatch in that manner on implicit classes. It is bit confusing to me that class() doesn't report all the implicit classes (like the first error message below) and inherits() only admits what class() has told.

Note what the first error message below says and how the succesive calls for afun() work as methods are added to the generic.

If you set a class attribute then the picture changes. I do not see where this is documented, but it looks like once a class attribute is set, the implicit classes go away - per the last error message.

> afun <- function(x) UseMethod("afun",x)
> afun(matrix(1L,nc=1))

Error in UseMethod("afun", x) :
  no applicable method for 'afun' applied to an object of class   "c('matrix', 'integer', 'numeric')"
> afun.numeric <- function(x) cat("numeric",x,"\n")
> afun(matrix(1L,nc=1))

numeric 1
> afun.integer <- function(x) cat("integer",x,"\n")
> afun(matrix(1L,nc=1))

integer 1
> afun.matrix <- function(x) cat("matrix",x,"\n")
> afun(matrix(1L,nc=1))

matrix 1
> afun.matrix <- function(x) NextMethod()
> afun(matrix(1L,nc=1))

integer 1
> afun.fooey <- function(x) NextMethod()
> my.mat <- matrix(1L,nc=1)
> afun(my.mat)

matrix 1
> class(my.mat) <- "fooey"
> afun(my.mat)

Error in NextMethod() : no method to invoke

> Anyway, an update to ?UseMethod would be welcome.

I wonder if "except with implicit" would improve over "with some interpolated" in ?class here:

" R UseMethod dispatches on the class as returned by class (with some interpolated classes: see the link)"

HTH, Chuck

> Or, documenting
> class() in only 1 place seems even better (more DRY principle).
> Thanks,
> H.

Charles C. Berry                            Dept of Family/Preventive Medicine
cberry at ucsd edu			    UC San Diego  La Jolla, CA 92093-0901

______________________________________________ mailing list
Received on Wed 05 Dec 2012 - 11:24:10 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 06 Dec 2012 - 11:12:41 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive