Dear UseRs,

Does anyone know when exactly the validity is checked in S4? Documentation is silent:(.
Here is a small example:

setClass("test1",representation(a="numeric"))
setMethod("initialize","test1",
          function(.Object,...){
                a<-runif(1)  ## here  slot "a" is initialized ##
callNextMethod(.Object,a=a,...) })

new("test1")
An object of class "test1"
Slot "a":
[1] 0.755

#next new subclass is created with an additional slot "b":
setClass("test2",contains="test1",representation(b="numeric")
## validity to test a==b ## ,validity=function(object){
             if(obj...@a!=object@b) print("values must be equal!!")
             else TRUE
         })

setMethod("initialize","test2",
          function(.Object,...){
              .Object<-callNextMethod(.Object,...)
              .obj...@b<-.obj...@a   ## here "b" is initialized ##
              .Object
          })

new("test2")
Error in if (obj...@a != obj...@b) print("values must be equal!!") else TRUE : argument is of length zero

This could mean only one thing, validity of "test2" is checked before it gets to point when b is initialized (i.e. in callNextMethod). But this is absurd (at leas from my point): callNextMethod constructs object of type "test" and should have nothing to do with validity of "test2". Shouldn't validity be checked only after the object is created?

Second my slot "a" is initialized in "test1"'s constructor and "b" must be 
assigned only after the callNextMethod as in above code. Seemingly unsolvable paradox? How to be?

Many thanks.

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to