I noticed this problem while working with R6 objects, but I can 
reproduce it using S3 classes.

If I define a method on the S3 superclass, the subclass doesn't inherit it.

``` r
library(methods)

SuperClass <- function(n) {
   result <- list(n=n)
   class(result) <- "SuperClass"
   result
}

setOldClass("SuperClass")

setGeneric("sampleSize",function(x) {standardGeneric("sampleSize")})
#> [1] "sampleSize"

setMethod("sampleSize","SuperClass", function(x) {x$n})

SubClass <- function (n) {
   result <- SuperClass(n)
   class(result) <- c("SubClass",class(result))
   result
}

setOldClass("SubClass")

example <- SubClass(7)
class(example)
#> [1] "SubClass"   "SuperClass"
is(example,"SuperClass")
#> [1] TRUE
methods("sampleSize")
#> [1] sampleSize,SuperClass-method
#> see '?methods' for accessing help and source code
sampleSize(example)
#> Error: unable to find an inherited method for function 'sampleSize' 
for signature 'x = "SubClass"'
```

<sup>Created on 2025-12-11 with [reprex 
v2.1.1](https://reprex.tidyverse.org)</sup>

I'm using R 4.5.2 and the same version of methods.

Is this a bug?

     --Russell


-- 
Russell Almond
https://ralmond.net/

        [[alternative HTML version deleted]]

______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

Reply via email to