From @ndre@@@b|@ette @end|ng |rom un|-due@de  Mon Oct  5 22:47:01 2020
From: @ndre@@@b|@ette @end|ng |rom un|-due@de (=?utf-8?B?QmzDpHR0ZSwgQW5kcmVhcw==?=)
Date: Mon, 5 Oct 2020 20:47:01 +0000
Subject: [Rd] S4 - inheritance changed by order of setClassUnion and setAs()
Message-ID: <98CDF167-D285-4074-BD2B-41BDC595599F@uni-due.de>

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]]



[Rd] S4 - inheritance changed by order of setClassUnion and setAs()
=?utf-8?B?QmzDpHR0ZSwgQW5kcmVhcw==?=