Skip to content
Prev 39872 / 63421 Next

self-referential representations in S4

The warning is there because all is not "just fine", in general and in 
particular not in your example.

If a superclass is not virtual, the prototype object for the new class 
must have a member of that class in the appropriate slot.  How could it 
do so in this case?  As a result, your class will return an invalid 
object from a call to new().

So one might argue that the current rules are too lax, and this should 
be an error.

The fundamental point is that S4 classes, as opposed to the new 
reference classes, don't deal in "references", self- or other.

Ways to deal with such recursive structures are discussed in section 9.7 
of Software for Data Analysis.

One version of what you were perhaps trying to do might, for a binary 
tree, be:

 > setClassUnion("MyNode", c("NULL", "vector"))
[1] "MyNode"
 >
 > setClass("FullNode", representation(left = "MyNode", right ="MyNode",
+   parent = "MyNode"))
[1] "FullNode"
 > setIs("FullNode", "MyNode")

Nodes can be full, a vector as a leaf, or empty.

John
On 4/19/11 3:10 PM, James Bullard wrote: