[Rd] S4 - inheritance changed by order of setClassUnion and setAs()

Blätte, Andreas @ndre@@@b|@ette @end|ng |rom un|-due@de
Mon Oct 5 22:47:01 CEST 2020


Dear colleagues,

there is a behaviour with S4 (virtual) classes that I find  very hard to understand: Depending on the position
of setAs(), the tree of inheritance changes.

This is my baseline example that defines the classes "grandma", "mother", "daughter" and a virtual
class "mr_x". For a new instance if "daughter", "mr_x" is betweeen "mother" and "grandma".

setClass("grandma", slots = c(a = "character"))
setClass("mother", slots = c(b = "matrix"), contains = "grandma")
setClass("daughter", slots = c(c = "list"), contains = "mother")
setClassUnion(name = "mr_x", members = c("daughter", "mother"))
setAs(from = "daughter", to = "grandma", def = function(from) new("grandma"))
is(new("daughter"))

[1] "daughter" "mother"   "mr_x"     "grandma"

Yet if I change the order of setAs() and setClassUnion(), this alters the pattern of inheritance.

setClass("grandma", slots = c(a = "character"))
setClass("mother", slots = c(b = "matrix"), contains = "grandma")
setClass("daughter", slots = c(c = "list"), contains = "mother")
setAs(from = "daughter", to = "grandma", def = function(from) new("grandma"))
setClassUnion(name = "mr_x", members = c("daughter", "mother"))
is(new("daughter"))

[1] "daughter" "mother"   "grandma"  "mr_x"

Is there a reasonable explanation for this behavior? I could not find any and I would appreciate
your help. If it is not an unintended behavior, I find it very confusing and hard to anticipate.

Kind regads
Andreas

--
Prof. Dr. Andreas Blätte
Professor of Public Policy and Regional Politics
University of Duisburg-Essen

	[[alternative HTML version deleted]]



More information about the R-devel mailing list