[Rd] Validity glitch when contains="matrix"

Martin Maechler maechler at stat.math.ethz.ch
Thu May 6 13:09:08 CEST 2010


>>>>> Daniel Murphy <chiefmurphy at gmail.com>
>>>>>     on Wed, 5 May 2010 22:08:06 -0700 writes:

    > Sirs:
    > My validity function did not run when my class contains="matrix". But if I
    > first define the class with contains="numeric", then define it again
    > with contains="matrix", validity runs. Here's the session:

    >> f <- function(object) "BAD CLASS" # force error to
    >> setClass("A", contains="matrix", validity=f)
    > [1] "A"
    >> new("A",as.matrix(1)) # should generate a validity error, does not
    > An object of class “A”
    > [,1]
    > [1,]    1
    >> setClass("B", contains="numeric", validity=f)
    > [1] "B"
    >> new("B",1) # generates the error
    > Error in validObject(.Object) : invalid class "B" object: BAD CLASS
    >> setClass("B", contains="matrix", validity=f)
    > [1] "B"
    >> new("B",as.matrix(1)) # generates the error
    > Error in validObject(.Object) : invalid class "B" object: BAD CLASS

The above behavior is clearly a bug.
The correct behavior *should* indeed show a validity error even
for class "A" above.
You are welcome to submit a formal bug report ..

    > On the other hand, when I define the class with "matrix" in its
    > representation, validity is called, no work-around necessary:

Yes, but that's really something different,
and I can imagine situations, where the  "contains" is very
preferable, as you get all the methods for "matrix" for free.


    >> setClass("C", representation(a="matrix"), validity=f)
    > [1] "C"
    >> new("C",a=as.matrix(1)) # error, as desired
    > Error in validObject(.Object) : invalid class "C" object: BAD CLASS

    > Should I
    > 1) always put "matrix" into the setClass representation argument instead of
    > the contains argument, or
    > 2) use contains="numeric", put the matrix's dims and dimnames attributes
    > into slots, and rely on a constructor to populate the instance?

Well, one can go the very long and "stable" way as we did in the 
Matrix package...

I'm not sure I would recommend that for you in your situation.
...
not the least because you *could* use the Matrix package if you
want to use such formal matrices with its thousands of methods.

Martin Maechler,
ETH Zurich

    > Option 2 seems most "stable".

    > Thanks,
    > Dan Murphy

    > Windows Vista, R version 2.11.0 (2010-04-22)

    > [[alternative HTML version deleted]]

    > ______________________________________________
    > R-devel at r-project.org mailing list
    > https://stat.ethz.ch/mailman/listinfo/r-devel



More information about the R-devel mailing list