[Rd] is(x, "parent") returns FALSE when class(x) is c("child", "parent") (PR#10549)

Tony Plate tplate at acm.org
Sat Jan 5 20:11:46 CET 2008


maechler at stat.math.ethz.ch wrote:
>>>>>> "TimH" == timh  <timh at insightful.com>
>>>>>>     on Sat,  5 Jan 2008 02:05:08 +0100 (CET) writes:
>>>>>>             
>
>     TimH> is() does not catch parent S3 classes:
>
>     >> library(splines)
>     >> temp <- bs(1:99, df=5)
>     >> class(temp)
>     TimH> [1] "bs"    "basis"
>     >> is(temp, "basis")
>     TimH> [1] FALSE
>
>     TimH> In contrast, is() does catch parent S4 classes:
>     >> library(copula)
>     >> norm.cop <- ellipCopula("normal", param = c(0.5, 0.6, 0.7),
>     TimH> +                         dim = 3, dispstr = "un")
>     >> is(norm.cop, "copula")
>     TimH> [1] TRUE
>     >> class(norm.cop)
>     TimH> [1] "normalCopula"
>
> Yes, that's all correct, but where's the bug?
>
> S3 has *NO* class definitions, so how can there be class
> inheritance?
>   
These types of statements make me really confused (and I suspect far 
more than necessary).  Isn't 'inherits()' an S3 function?  (at least 
it's not in package "methods", and it works with S3 classes, while 'is' 
is in the "methods" package).

The inherits documentation says:
Details:

     Many R objects have a 'class' attribute, a character vector giving
     the names of the classes from which the object _inherits_. 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"').  (Functions
     'oldClass' and 'oldClass<-' get and set the attribute, which can
     also be done directly.)

But if S3 has no class definitions, and that fact precludes class 
inheritance, how come the inherits() functions exists and works with S3 
"classes" and inherits() documentation talks about classes and inheritance?

And it looks like 'inherits()' does what (I think) Tim Hesterberg is 
looking for:

 > x <- structure(1, class=c("a", "ab", "abc"))
 > is(x, "a")
[1] TRUE
 > is(x, "ab")
[1] FALSE
 > inherits(x, "a")
[1] TRUE
 > inherits(x, "ab")
[1] TRUE
 >


> There's many reasons to go from S3 to S4, and the lack of class
> definitions in S3 is an important one...
>
> Now, still being curious:  Are you implicitly saying that in S-plus,
> is() behaves differently, namely 
> ``as if S3 classes would exist?'' (:-)
>   
Is S-PLUS 7.0, I see the following behavior:

 > x <- structure(1, class=c("a", "ab"))
Warning in checkOldClass(c("a", "ab")): No formal definition of 
old-style inheritance; consider setOldClass(c("a",
 "ab"))
 > inherits(x, "a")
[1] T
 > inherits(x, "ab")
[1] T
 > is(x, "a")
[1] T
 > is(x, "ab")
[1] T
 >

Which would make it appear that in S-PLUS, for objects with S3 classes 
the function "is" uses the definition of the concept "inherits" that 
appears in the R documentation for the function "inherits" :-)  (But, 
apparently, S3 classes don't exist, so does that make R an imaginary 
language except when one uses S4 classes? :-)

-- Tony Plate

> [Of course, I understand what you mean, and I agree that
>  normally, the S3  class attribute  c("ch", "pr") means that "ch"
>  is conceptually a child of "pr".
>  
>  However, there's at least one exception in R {which I have found a
>  bit unfortunate, but never followed up}:
>
>    > class(Sys.time())
>    [1] "POSIXt"  "POSIXct"
> ]
>
> Regards,
> Martin
>
>     TimH> Version:
>     .....
>     TimH> version.string = R version 2.6.1 (2007-11-26)
>     .....
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>
>



More information about the R-devel mailing list