Search code examples
rr-s4

R S4 class union of class unions


I am trying to establish a union of class unions to facilitate method dispatch. The following reprex does exactly what I want when executed in the global environment, but as soon as I put this code into a package, the last line, f(new("a")), throws an error that it is unable to find an inherited method.

setClass("x", slots = list(slot ="character"))
setClass("y", slots = list(slot ="character"))
setClass("a", slots = list(slot ="character"))
setClass("b", slots = list(slot ="character"))
setClassUnion("xy", c("x", "y"))
setClassUnion("ab", c("a", "b"))
setClassUnion("xyab", c("xy", "ab"))
setGeneric("f", function(object, ...) standardGeneric("f"))
setMethod("f", "xyab", function(object, ...) print("hi!"))

## print's "hi!" as expected
f(new("a"))

what am I missing?

to facilitate reproduction in a fresh R session, this reproduces the issue:

library(devtools)
fn <- "codefile.R"
writeLines(
    c(
        "setClass('x', slots = list(slot ='character'))",
        "setClass('y', slots = list(slot ='character'))",
        "setClass('a', slots = list(slot ='character'))",
        "setClass('b', slots = list(slot ='character'))",
        "setClassUnion('xy', c('x', 'y'))",
        "setClassUnion('ab', c('a', 'b'))",
        "setClassUnion('xyab', c('xy', 'ab'))",
        "setGeneric('f', function(object, ...) standardGeneric('f'))",
        "setMethod('f', 'xyab', function(object, ...) print('hi!'))"
    ),
    con = fn
)

package.skeleton(code_files = "codefile.R")
devtools::load_all("anRpackage")
f(new("a"))

Solution

  • Per R-devel, this is resolved in 4.0:

    https://r.789695.n4.nabble.com/Possible-Regression-in-setClassUnion-between-3-5-0-and-3-6-0-td4761507.html