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"))
Per R-devel, this is resolved in 4.0: