Bug 14284 - Validity function did not run when class contains="matrix"
Validity function did not run when class contains="matrix"
Status: RESOLVED FIXED
Product: R
Classification: Unclassified
Component: S4methods
R 2.11.0
All Windows 64-bit
: P5 normal
Assigned To: R-core
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2010-05-06 14:43 UTC by Dan Murphy
Modified: 2010-05-08 22:33 UTC (History)
2 users (show)

See Also:


Attachments
test for multiple superclasses--fails by taking the a1 object as nrow arg. (200 bytes, text/plain)
2010-05-07 17:11 UTC, John Chambers
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Dan Murphy 2010-05-06 14:43:49 UTC
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

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

- Dan
Comment 1 Martin Maechler 2010-05-07 09:58:48 UTC
It's the same problem, with e.g.,  "ts"  instead of "matrix".

and as John Chambers wrote on R-devel, it's indeed in how the initialize() method works in these two cases, namely failing to call validMethod().
{{and it may be worthwhile thinking about moving the validMethod() call away   from initialize()  ... 
}}
Comment 2 John Chambers 2010-05-07 17:11:12 UTC
Created attachment 1103 [details]
test for multiple superclasses--fails by taking the a1 object as nrow arg.
Comment 3 John Chambers 2010-05-07 17:13:04 UTC
There are several related issues in the initialize() methods for "matrix", "array" and "ts", all of which are ancient and don't follow the general rules correctly.  For example, if a class contains both "matrix" and another superclass, an attempt to initialize with an object from the other superclass will fail because the method interprets unnamed arguments as for the matrix() function. (test code attached)
Comment 4 John Chambers 2010-05-08 22:33:20 UTC
The original report of not checking validity should be fixed in 2.11.0-patched by revision 51947 (for subclasses of "matrix", "array" and "ts")

A more general approach that also fixes the related problem of not handling mix-ins with these classes is being tested, but as it's rather more radical it seemed better to keep it for r-devel.  (Rev 51947 was not ported to r-devel, but I have no objections if it's wanted as a temporary fix.)