Mikael Jagan
2025-Dec-12 13:49 UTC
[Rd] S4 generic functions don't respect S3 class hierarchies.
S4 is "formal", so you need to declare inheritance:
setOldClass(c("SubClass", "SuperClass"))
This usage (first argument of length greater than one) is documented by
help("setOldClass") including among the examples where analogously I
see
setOldClass(c("mlm", "lm"))
and there are more examples in the list of old classes defined by
'methods',
exported as '.OldClassesList'.
Mikael
> Date: Thu, 11 Dec 2025 17:10:28 -0500
> From: Russell Almond<russell.g.almond at gmail.com>
>
> 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]]
Russell Almond
2025-Dec-12 16:06 UTC
[Rd] S4 generic functions don't respect S3 class hierarchies.
Thanks.? That's helpful.? It also works in my use case with R6 classes, even if I don't include the "R6Class" in the setOldClass definition. ??? --Russell On 12/12/25 08:49, Mikael Jagan wrote:> S4 is "formal", so you need to declare inheritance: > > ??? setOldClass(c("SubClass", "SuperClass")) > > This usage (first argument of length greater than one) is documented by > help("setOldClass") including among the examples where analogously I see > > ??? setOldClass(c("mlm", "lm")) > > and there are more examples in the list of old classes defined by > 'methods', > exported as '.OldClassesList'. > > Mikael > > >> Date: Thu, 11 Dec 2025 17:10:28 -0500 >> From: Russell Almond<russell.g.almond at gmail.com> >> >> 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]] >-- Russell Almond https://ralmond.net/ [[alternative HTML version deleted]]