[Rd] S4 classes and objects -- fixed structure? No...

Roger D. Peng rdpeng at gmail.com
Mon Aug 28 16:13:29 CEST 2006


I think you're right---this shouldn't happen in theory, but it does because of 
the internal representation of S4 objects in R.

In R devel (to be 2.4.0), this changes and I believe your example will no longer 
work.

-roger

Jörg Beyer wrote:
> Hello.
> 
> Suppose you define a new S4-class, say
>>  setClass("track", representation(x="numeric", y="numeric"))
> 
> Don't worry if you have a deja vu, it's from the help page.
> Your new class is said to have a fixed structure: two slots, x, and y,
> and that should apply to all objects you construct as members of that class.
>>  tr <- new( "track" )
> 
> Now do the following:
>>  tr[ "ping" ] <- "pong"
>>  tr$bingo <- "bongo"
>>  tr[[ "blaa" ]] <- c( 200, 300 )
> 
> Of course you can use the well known operators to access these "list entries
> in a S4-class object":
>>  tr[ "blaa" ]
>>  etc. 
> 
> You see what can happen if you decide to do a bit stress testing. The
> question is not whether my examples makes sense or not. The question is if
> these examples should be possible at all.
> I wonder which is true,
> -- the theory (Chambers, 1998, p. 279ff; Venables and Ripley, 2000, p. 99ff:
> "... All objects in a [S4] class must have the same structure. ..."; etc.)
> -- or the actual implementation in R (see my example, which successfully
> violates the design principles of the language)
> 
> Bug, or feature? Any clarifications are appreciated -- it may be my
> half-cooked knowledge that I find this confusing and dangerous.
> 
> Best 
> 
> Joerg Beyer
> 
> P.S.: 
> Oh, the specs: PowerMac G4/400 PCI -- 1GB RAM -- Mac OS X 10.4.6 -- R 2.2.1
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
> 

-- 
Roger D. Peng  |  http://www.biostat.jhsph.edu/~rpeng/




More information about the R-devel mailing list