[R] Question on S7 base classes

Duncan Murdoch murdoch@dunc@n @end|ng |rom gm@||@com
Mon Jan 5 20:16:27 CET 2026


On 2026-01-04 7:35 p.m., Bert Gunter wrote:
> Could someone explain the following behavior to me, which I find
> inconsistent and confusing:
> 
>> inherits(1L, "integer")
> [1] TRUE    **as expected. S3 class**
>> inherits(1L, class_integer)
> [1] TRUE  ** as expected. S7 base class**
> 
>> 

> [1] FALSE   ** There is no "double" class in S3
>> inherits(1.0, "numeric")
> [1] TRUE ** doubles are S3 class "numeric". This is (sort of) documented**
> 
> BUT ...
>> inherits(1.0, class_double)
> [1] FALSE  ** because there is no S3 class "double" ?? **
> 
>> inherits(1.0, class_numeric)
> Error in inherits(1.2, class_numeric) :
>    'what' must be a character vector or an object with a nameOfClass() method
> **class_numeric is an S7 union, not an S7 base class ?**
> 
> So if 1.0 is not class_double, what base class is it? And how should I test
> for the class of a non-integer numeric? And what is class_double then?
> 
> My apologies if this is obvious, but as I said, I find this confusing.

It's documented that

   inherits(1.0, class_double)

should execute

   inherits(1.0, "double")

because nameOfClass(class_double) is "double".	So things are consistent 
because you found

   inherits(1.0, "double") == inherits(1.0, class_double)

And nameOfClass(class_numeric) is NULL, because the S7 authors haven't 
given a way for an S7 union to have an S3 class name.

So I think you are right to be confused, but the only solution I can 
think of (allowing S7 unions to have S3 names) might be seen as just too 
much trouble.  Are there any other cases where an S7 union corresponds 
to an S3 class?

Duncan Murdoch



More information about the R-help mailing list