class() bug when used within a validity method

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

class() bug when used within a validity method

Pages, Herve
Hi,

This is a long-standing bug where 'class(object)' does not
return the actual class of 'object' when used inside a validity
method. Instead it seems to return the class for which the validity
method is defined. For example:

   setClass("A", slots=c(stuff="ANY"))

   setValidity("A", function(object) {
     cat("validating an object of class:", class(object), "\n")
     TRUE
   })

Then:

   a <- new("A")

   validObject(a)
   # validating an object of class: A
   # [1] TRUE

which is OK. But trying to validate an object that belongs to a
subclass of A leads to a big surprise:

   setClass("B", contains="A")

   b <- new("B")

   class(b)
   # [1] "B"

   validObject(b)
   # validating an object of class: A
   # [1] TRUE

The class of object 'b' as seen by class() is wrong.

Note that this doesn't happen if A is defined as a VIRTUAL class.

Cheers,
H.

--
Hervé Pagès

Program in Computational Biology
Division of Public Health Sciences
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N, M1-B514
P.O. Box 19024
Seattle, WA 98109-1024

E-mail: [hidden email]
Phone:  (206) 667-5791
Fax:    (206) 667-1319
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: class() bug when used within a validity method

Pages, Herve
On 8/19/19 16:23, Pages, Herve wrote:
...
> Note that this doesn't happen if A is defined as a VIRTUAL class.

To be precise, when A is a VIRTUAL class, it requires at least
one additional level of class extension to break class():

   setClass("A", contains="VIRTUAL", slots=c(stuff="ANY"))
   setValidity("A", function(object) {
     cat("validating an object of class:", class(object), "\n")
     TRUE
   })
   setClass("B", contains="A")
   setClass("C", contains="B")

Then:

   c <- new("C")
   validObject(c)
   # validating an object of class: B
   # [1] TRUE

H.


--
Hervé Pagès

Program in Computational Biology
Division of Public Health Sciences
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N, M1-B514
P.O. Box 19024
Seattle, WA 98109-1024

E-mail: [hidden email]
Phone:  (206) 667-5791
Fax:    (206) 667-1319
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel