[Rd] Discrepancy between is.list() and is(x, "list")

Berry, Charles ccberry @end|ng |rom uc@d@edu
Tue Mar 26 17:30:58 CET 2019


In the case of inherits (at least) this seems intended.

The help page says:

"If the object does not have a class attribute, it has an implicit class..."

which I take to mean that if an object does have a class attribute it does not also have an implicit class.

The behavior you noted below will apply to other types bearing implicit classes. For example:

> inherits(1.0, "numeric")
[1] TRUE
> inherits(structure(1.0, class="myclass"), "numeric")
[1] FALSE
> 

I think this is reasonable behavior. Consider the "Date" class, which stores values as "numeric":

> class(Sys.Date())
[1] "Date"
> inherits(Sys.Date(),"numeric")
[1] FALSE
> class(unclass(Sys.Date()))
[1] "numeric"
> Sys.Date()%%2
Error in Ops.Date(Sys.Date(), 2) : %% not defined for "Date" objects
> 

Letting the modulus operator (as one example) inherit the numeric class here could create problems.

Of course for classes that should inherit the implicit type, it can be explicitly added to the end of the class() vector by its constructor.

HTH,

Chuck



> On Mar 25, 2019, at 8:27 PM, Abs Spurdle <spurdle.a using gmail.com> wrote:
> 
>> I have noticed a discrepancy between is.list() and is(x, “list”)
> 
> There's a similar problem with inherits().
> 
> On R 3.5.3:
> 
>> f = function () 1
>> class (f) = "f"
> 
>> is.function (f)
> [1] TRUE
>> inherits (f, "function")
> [1] FALSE
> 
> I didn't check what happens with:
>> class (f) = c ("f", "function")
> 
> However, they should have the same result, regardless.
> 
>> Is this discrepancy intentional?
> 
> I hope not.
> 
> 	[[alternative HTML version deleted]]
> 



More information about the R-devel mailing list