[R] Why 'validity' is not called? (S4)

Peng Yu pengyu.ut at gmail.com
Mon Oct 26 19:35:31 CET 2009


I thought that 'validity' defined in 'setClass' should be called in
'new'. Could somebody let me know why 'validity' is not called? How to
make it be called?

> setClass(
+     Class='A',
+     representation=representation(
+         x='numeric'
+         ),
+     validity=function(object){
+       cat("~~~ A: inspector ~~~\n")
+       if(object at x<0){
+         stop("[A:validation] object at x<0")
+       }
+       return(T)
+     }
+     )
[1] "A"
> setMethod(
+     f='initialize',
+     signature='A',
+     definition=function(.Object,x){
+       cat("~~~ A: initializator ~~~\n")
+       .Object at x=x
+       return(.Object)
+     }
+     )
[1] "initialize"
> new(Class='A', x=10)
~~~ A: initializator ~~~
An object of class \u201cA\u201d
Slot "x":
[1] 10




More information about the R-help mailing list