Hello, i am using classes and want to force an validity check when an object is created.
platform i686-pc-linux-gnu arch i686 os linux-gnu system i686, linux-gnu status major 1 minor 7.1 year 2003 month 06 day 16 language R Following piece of code works fine, just like I expected it to ---------- > setClass("Foo", representation(woo = "numeric")) [1] "Foo" > validFooObject <- function(object) { + if([EMAIL PROTECTED] > 0) return(TRUE) + else return("warning: negative value for woo")} > setValidity("Foo", validFooObject) [1] "Foo" > new("Foo", woo = 1) # all is fine An object of class "Foo" Slot "woo": [1] 1 > new("Foo", woo = -1) # ok, negative value Error in validObject(.Object) : Invalid "Foo" object: warning: negative value for woo --------- Now, if i define a initialize method for Foo, things change (R had been restarted) --------- > setClass("Foo", representation(woo = "numeric")) [1] "Foo" > validFooObject <- function(object) { + if([EMAIL PROTECTED] > 0) return(TRUE) + else return("warning: negative value for woo")} > setValidity("Foo", validFooObject) [1] "Foo" > setMethod("initialize", "Foo", function(.Object, woo){ + [EMAIL PROTECTED] = woo + .Object}) [1] "initialize" > new("Foo", woo = 1) # all is fine An object of class "Foo" Slot "woo": [1] 1 > new("Foo", woo = -1) # ! no warning, object created! An object of class "Foo" Slot "woo": [1] -1 --------- How do i force an validity check, when an initalize method has been set? Thanks for your help. Greetings, Thomas Stabla
setClass("Foo", representation(woo = "numeric")) validFooObject <- function(object) { if([EMAIL PROTECTED] > 0) return(TRUE) else return("warning: negative value for woo")} setValidity("Foo", validFooObject) new("Foo", woo = 1) # all is fine new("Foo", woo = -1) # ok, negative value ############################################### setClass("Foo", representation(woo = "numeric")) validFooObject <- function(object) { if([EMAIL PROTECTED] > 0) return(TRUE) else return("warning: negative value for woo")} setValidity("Foo", validFooObject) setMethod("initialize", "Foo", function(.Object, woo){ [EMAIL PROTECTED] = woo .Object}) new("Foo", woo = 1) # all is fine new("Foo", woo = -1) # ! no warning message is produced !
______________________________________________ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help