[Rd] S4 'object is not subsettable' in prototype

Martin Maechler maechler at stat.math.ethz.ch
Mon Aug 21 08:49:55 CEST 2006


>>>>> "JMC" == John Chambers <jmc at r-project.org>
>>>>>     on Sun, 20 Aug 2006 20:20:45 -0400 writes:

    JMC> When I was introducing the special type for S4 objects,
    JMC> my first inclination was to have length(x) for those
    JMC> objects be either NA or an error, along the lines that
    JMC> intuitively length(x) means "the number of elements in
    JMC> the vector-style object x".  However, that change
    JMC> quickly was demonstrated to need MANY revisions to the
    JMC> current code.

    JMC> As a compromise, my preference would be that S4 objects
    JMC> have length 0.  That was true before the S4 type was
    JMC> introduced, and if memory serves, that was what we
    JMC> expected in early S for objects that were not vectors.
    JMC> Maybe we can try that out, at least for 2.4.1.

That would look more sensible to me as well. Of course this only
applies to S4 objects that are not of an S4 class which has an own
length() method.

BTW, Martin Morgan's original example now "works" in R-devel,
since the (unconditional availability of the) new 'S4' type has
allowed to logically simplify str() there.
"works" in double quotes because the prototype part does not yet really
look nice..

    JMC> But as often happens, we have to deal with the fact
    JMC> that R has many authors.  That's usually a good thing,
    JMC> but it does mean that different corners of the code
    JMC> will be locally sensible, but not globally consistent.

(nice saying; I like the wording ..)

    JMC> (Having just spent the best part of a day "debugging" a
    JMC> problem that turned out to be the result of an S3
    JMC> method for trunc() in base that ignored the documented
    JMC> number of arguments, the inconsistencies are on my mind
    JMC> at the moment.)

(I'm interested in the details, if you can say a bit more; 
 maybe these are not relevant for R-devel ..)

    JMC> Martin Maechler wrote:
    >>>>>>> "Seth" == Seth Falcon <sfalcon at fhcrc.org> on Fri, 18
    >>>>>>> Aug 2006 09:54:54 -0700 writes:
    >>>>>>> 
    >>
    Seth> Martin Maechler <maechler at stat.math.ethz.ch> writes:
    >> >> I agree you've found an "infelicity" in the current
    >> setup, >> since
    >> >> 
    >> >> > setClass("A", representation(x="numeric")) >> [1]
    >> "A" >> > getClass("A")@prototype >> <S4 Type Object> >>
    >> attr(,"x") >> numeric(0) >> >
    >> length(getClass("A")@prototype) >> [1] 1 >> >
    >> getClass("A")@prototype[1] >> Error in
    >> getClass("A")@prototype[1] : object is not subsettable
    >> >> > 
    >> >> 
    >> >> It's not clear yet to me *what* should be changed.  >>
    >> For my taste, if length(object) returns 1, >> I'd
    >> typically want that object[1] should be valid; >> but I
    >> think we already have some (peculiar) exceptions to that
    >> >> rule.
    >> 
    Seth> I disagree with the notion that length and "[" should
    Seth> be tightly coupled.  An object having length doesn't
    Seth> have to mean that subsetting makes any sense.  For
    Seth> example, an environment has length, but what should
    Seth> env1[1] return?  And with S4 classes, there are many
    Seth> such examples where "[i]" won't make sense.
    >>  You are right.  I was rather thinking of "basic" objects
    >> rather than highly structured objects such as S4 classes.
    >> 
    >> Note that for me, an environment belongs into the
    >> category of "peculiar" exceptions ;-)
    >> 
    >> Martin
    >> 
    Seth> + seth



More information about the R-devel mailing list