Dear useRs and wizaRds,

I am currently developing a set of functions using S4 classes. On the way I 
encountered the problem exemplified with the code below. For some reason the 
'validity' method does not seem to work, i.e. does not check for errors in the 
specification of the slots of the defined class. Any hints?

My understanding of the whole S4 system was that validity checks are made 
*after* class initialization. Is that correct?

Thanks a lot in advance!

PS. Session info:

R version 2.5.1 (2007-06-27) 
i386-pc-mingw32 

locale:
LC_COLLATE=Polish_Poland.1250;LC_CTYPE=Polish_Poland.1250;LC_MONETARY=Polish_Poland.1250;LC_NUMERIC=C;LC_TIME=Polish_Poland.1250

attached base packages:
[1] "stats"     "graphics"  "grDevices" "utils"     "datasets"  "methods"  
[7] "base"     
> 




-b-e-g-i-n--r--c-o-d-e-

setClass( "someclass", representation(v="numeric", l="character"),
    prototype( v=numeric(0),
        l=character(0) )
    )

setMethod("initialize", "someclass",
function(.Object, v=numeric(0), l=character(0))
{
    # strip the vector names
    cv <- v
    cl <- l
    names(cv) <- NULL
    names(cl) <- NULL
    rval <- .Object
    [EMAIL PROTECTED] <- cv
    [EMAIL PROTECTED] <- cl
    rval
} )

# at this point this should be OK
o <- new("someclass", v=1:2, l=letters[1:3])
o

# check validity
f <- function(object)
{
    rval <- NULL
    if( length([EMAIL PROTECTED]) != length([EMAIL PROTECTED]) )
        rval <- c( rval, "lengths dont match")
    if( is.null(rval) ) return(TRUE)
    else return(rval)
}
 
# this should return error description
f(o)


# define the validity method
setValidity( "someclass", f)

# this should return an error
new("someclass", v=1:2, l=letters[1:3])

# but it doesn't...

-e-n-d--r--c-o-d-e-




____________________________________
Michal Bojanowski
ICS / Department of Sociology
Utrecht University
Heidelberglaan 2; 3584 CS Utrecht
Room 1428
m.j.bojanowski at uu dot nl
http://www.fss.uu.nl/soc/bojanowski/


        [[alternative HTML version deleted]]

______________________________________________
[email protected] 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