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

J ö rg Beyer Beyerj at students.uni-marburg.de
Mon Aug 28 15:55:48 CEST 2006


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




More information about the R-devel mailing list