Skip to content
Prev 33648 / 63424 Next

S4: inheritance of validity methods?

On Tue, 18 Aug 2009 18:58:27 +0200, Martin Morgan <mtmorgan at fhcrc.org>  
wrote:
yes, indeed. At first glance seems as a nice trick, but:
[1] "C"
B

yah... same story at a different level:(.
This one has similar problem:


setGeneric(".validity", useAsDefault=function(object) {
     cat(class(object), "\n")
     TRUE
})

  setClass("A", representation(a="numeric", n="integer"),  
validity=.validity)
  setClass("B", representation("A", b="numeric"), validity=.validity)
  setClass("C", representation("B", c="numeric"), validity=.validity)
A
B
A
B
C

?validObject says:

      Validity testing takes place *bottom up*(...)for each of the classes  
that this
      class extends (the ?superclasses?), the explicit validity method
      of that class is called, if one exists.  Finally, the validity
      method of 'object''s class is called, if there is one.

This is ectly what happens above when A,B,C is printed.

It seams like there is no way to implement a general function which would  
be called for validation of each children class. Moreover, if I attempt to  
rewrite manually the validity method for each subclass taht would also not  
work, because validity is tested "bottom up". validObject for A is called  
anyway - and is obviously not valid:


  setClass("A", representation(a="numeric", n="integer"),
           validity=function(object) length(slotNames(object))==object at n+1
           )

  setClass("B", representation(b="numeric"), contains="A",
           validity=function(object) length(slotNames(object))==object at n+1
           )
Error in validObject(.Object) : invalid class "B" object: FALSE

So there is no way to validate at all!!!
--