[Rd] Class union of custom classes

pik@pp@@devei m@iii@g oii gm@ii@com pik@pp@@devei m@iii@g oii gm@ii@com
Sun Jul 3 13:02:50 CEST 2022


Dear all,

 

This code, mostly copied from the setClassUnion help page, works as
described in the documentation:

 

# test 1

setClassUnion("maybeNumber", c("numeric", "logical"))

setClass("withId", contains = "maybeNumber", slots = c(id = "character"))

w1 <- new("withId", 1.2, id = "test 1")

 

However, the following three tests do not work:

 

# test 2

setClass("foo", slots = list(xb = "logical"))

setClassUnion("maybeNumber", c("numeric", "foo"))

setClass("withId", contains = "maybeNumber", slots = c(id = "character"))

w1 <- new("withId", 1.2, id = "test 2")

 

# test 3

setClass("foo", slots = list(xb = "logical"))

setClassUnion("maybeNumber", c("numeric"))

setIs("foo", "maybeNumber")

setClass("withId", contains = "maybeNumber", slots = c(id = "character"))

w1 <- new("withId", 1.2, id = "test 3")

 

# test 4

setClass("foo", contains = "logical")

setClassUnion("maybeNumber", c("numeric", "foo"))

setClass("withId", contains = "maybeNumber", slots = c(id = "character"))

w1 <- new("withId", 1.2, id = "test 4")

 

All three return:

 

  Error in initialize(value, ...) : 

      'initialize' method returned an object of class "numeric" instead of
the required class "withId"

 

The error comes from:

 

traceback()

3: stop(gettextf("'initialize' method returned an object of class %s instead
of the required class %s", 

       paste(dQuote(class(value)), collapse = ", "),
dQuote(class(.Object))), 

       domain = NA)

2: initialize(value, ...)

1: new("withId", 1.2, id = "test 2")

 

I would expect tests 2-4 to work similarly to the first test. Is the above
error the intended behavior of setClassUnion? I do not see anything that
would prevent this in the documentation. Is there something I am missing
here?

 

Any help would be very much appreciated!

 

Kind regards,

Pantelis


	[[alternative HTML version deleted]]



More information about the R-devel mailing list