[Rd] optional setValidity()

Sklyar, Oleg (MI London) osklyar at maninvestments.com
Wed May 7 17:30:26 CEST 2008


sorry I forgot the return statement, it should be
if (!object at .validate) return(TRUE)

Dr Oleg Sklyar
Technology Group
Man Investments Ltd
+44 (0)20 7144 3803
osklyar at maninvestments.com 

> -----Original Message-----
> From: r-devel-bounces at r-project.org 
> [mailto:r-devel-bounces at r-project.org] On Behalf Of Sklyar, 
> Oleg (MI London)
> Sent: 07 May 2008 16:13
> To: Robin Hankin; R-devel at r-project.org
> Subject: Re: [Rd] optional setValidity()
> 
> You could add a flag to your class and check if it set as a 
> first thing in the validity as in the example below:
> 
> setClass("foo",
>   representation("numeric", .validate="logical"),
>   prototype(.validate=TRUE),
>   validity=function(object) {
>     if (!object at .validate) TRUE
>     ## do lengthy checks
>   }
> )
> 
> setGeneric("foo", function(x, ...) standardGeneric("foo")) 
> setMethod("foo", signature(x="numeric"),
>   function(x, ..., validate=TRUE) new("foo", ..., .validate=validate)
> )
> 
> a = foo(runif(10), validate=FALSE)
> ## or
> b = new("foo", runif(10), .validate=FALSE) ## or do validate 
> d = foo(runif(10)) e = new("foo", runif(10))
> 
> The downside is that you carry unnecessary information around 
> in your objects.
> 
> Best,
> 
> Dr Oleg Sklyar
> Technology Group
> Man Investments Ltd
> +44 (0)20 7144 3803
> osklyar at maninvestments.com 
> 
> > -----Original Message-----
> > From: r-devel-bounces at r-project.org
> > [mailto:r-devel-bounces at r-project.org] On Behalf Of Robin Hankin
> > Sent: 07 May 2008 15:44
> > To: R-devel at r-project.org
> > Subject: [Rd] optional setValidity()
> > 
> > Hi
> > 
> > 
> > Suppose I have an S4 class "foo" and a validity checking function  
> > ".checkfoo()":
> > 
> > setClass("foo",  representation=representation("numeric"))
> > setValidity("foo" , .checkfoo)
> > 
> > is fine; in my application, .checkfoo() verifies that a bunch of 
> > necessary conditions are met.
> > 
> > But .checkfoo() is very time consuming and I want to give users the 
> > option of switching it off.
> > 
> > Most foo objects that one deals with fall into two or three 
> standard 
> > types and in these cases one doesn't need to execute
> >  .checkfoo() because one can show algebraically that the conditions 
> > are automatically met.
> > 
> > But OTOH, I want the check to be performed "by default" to 
> stop anyone 
> > (me) from being too clever and defining a non-standard foo 
> object that 
> > doesn't meet .checkfoo().
> > 
> > What is best practice here?
> > 
> > Are there any examples I could copy?
> > 
> > 
> > 
> > 
> > 
> > --
> > Robin Hankin
> > Uncertainty Analyst and Neutral Theorist, National Oceanography 
> > Centre, Southampton European Way, Southampton
> > SO14 3ZH, UK
> >   tel  023-8059-7743
> > 
> > ______________________________________________
> > R-devel at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-devel
> > 
> 
> 
> **********************************************************************
> The contents of this email are for the named 
> addressee(s...{{dropped:22}}
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
> 


**********************************************************************
The contents of this email are for the named addressee(s...{{dropped:22}}



More information about the R-devel mailing list