[R] Object validation and formal classes
Robert Gentleman
rgentlem at jimmy.harvard.edu
Fri Jan 30 02:04:32 CET 2004
On Thu, Jan 29, 2004 at 09:30:19PM +0100, Torsten Steuernagel wrote:
> I'm using R 1.8.1 (Win32, Linux) and have some difficulties using
> validation functions for S4 classes. The problem is if I specify a
> validation function with setValidity("myclass", validate.myclass) object
> validation is only performed when I create an instance using
> new("myclass"), or when I explicitly call validObject(x) where x is of
> class "myclass", of course.
>
> According to the reference docs, I would expect that validation always
> takes place implicitly when I manipulate an object of "myclass". This
> especially includes implicit validation if I change slots directly, i.e.
> x at myslot <- 1:50 should call my validation function. Unfortunately, it
> isn't called and instead of raising an error and leaving the object
> unchanged in case validation fails, the object is always changed no
> matter what I assign to the slot.
There are some efficiency issues that prevent constant checking (at
least at the present time). There are also some other issues that
need to be adequately addressed too. For example, suppose I had an
object with two slots
a - a character string
b - the number of characters
and I set my validity checker to make sure that the length of the
string is the number in b. Now that basically means that I can never
change the string (except to other strings of the same length) if
validity checking happened after every change. I somehow need
changing both a and b to be instantaneous (which they currently are
not). We have not really gone far enough down that path yet to know
what the right thing is, but we are working on it. So for now
validity checking occurs at a few specific points and if/when you
ask for it.
Robert
>
> I'm not sure if I'm missing something here or if I just didn't get the point
> of validation functions, but I believe there must be a way to assure that
> an object is in a consistent state.
>
> Thanks for your help,
>
> Torsten
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
--
+---------------------------------------------------------------------------+
| Robert Gentleman phone : (617) 632-5250 |
| Associate Professor fax: (617) 632-2444 |
| Department of Biostatistics office: M1B20 |
| Harvard School of Public Health email: rgentlem at jimmy.harvard.edu |
+---------------------------------------------------------------------------+
More information about the R-help
mailing list