From ggrothendieck at gmail.com  Mon Aug 28 16:08:53 2006
From: ggrothendieck at gmail.com (Gabor Grothendieck)
Date: Mon, 28 Aug 2006 10:08:53 -0400
Subject: [Rd] S4 classes and objects -- fixed structure? No...
In-Reply-To: <C118C284.19D3%Beyerj@students.uni-marburg.de>
References: <C118C284.19D3%Beyerj@students.uni-marburg.de>
Message-ID: <971536df0608280708w3580b855nf76b45b0807931ae@mail.gmail.com>

Under R 2.3.1 these work as you indicate but under R 2.4.0
they all give errors:

>  setClass("track", representation(x="numeric", y="numeric"))
[1] "track"
>  tr <- new( "track" )
>  tr[ "ping" ] <- "pong"
Error in "[<-"(`*tmp*`, "ping", value = "pong") :
        object is not subsettable
>  tr$bingo <- "bongo"
Error: cannot coerce type S4 to list vector
In addition: Warning message:
Coercing LHS to a list
>  tr[[ "blaa" ]] <- c( 200, 300 )
Error in "[[<-"(`*tmp*`, "blaa", value = c(200, 300)) :
        object is not subsettable
>  tr[ "blaa" ]
Error in tr["blaa"] : object is not subsettable
> R.version.string
[1] "R version 2.4.0 Under development (unstable) (2006-08-08 r38825)"


On 8/28/06, J?rg Beyer <Beyerj at students.uni-marburg.de> 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
>




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