Skip to content

Object validation and formal classes

5 messages · Torsten Steuernagel, Robert Gentleman, John Chambers

#
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.

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
#
On Thu, Jan 29, 2004 at 09:30:19PM +0100, Torsten Steuernagel wrote:
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

  
    
#
It was never the intention that validity checking happen automatically 
on _every_ assignment of an object from the class--since those 
assignments take place frequently during evaluation of functions, the 
overhead would be unacceptable.  And as Robert points out, one needs to 
postpone validity checking until a set of mutually dependent changes is 
finished.

The "Programming with Data" description says that validity checking 
takes place on "permanent assignment".  But this was not written with R 
in mind, and the idea of permanent assignment is ambiguous in R.  It 
could mean all assignments into the Global environment, or it could mean 
on serializing (saving the workspace, e.g.).  If we agree on a useful 
interpretation, automatic validity checking might be reasonable in that 
sense in the future.
Torsten Steuernagel wrote:

            
#
On 30 Jan 2004 at 14:05, John Chambers wrote:

            
Thank's for this clarification. I'll try to find another solution. Could you 
comment on the "access" argument in "setClass()" ? From the 
"setClass()" reference I can only guess what that might be:

"access: Access list for the class.  Saved in the definition, but not
          currently used."

Is this intended to implement an access control mechanism similar to 
C++ like I stated in my previous message ? If that's the case I'll simply 
provide access/assign methods for the slots that need validation. Once 
access control is available I could hide those slots.
I must admit that I didn't look into "Programming with Data", which I 
don't have available right now, but I believe "S Programming" says the 
same. I think the example code in the reference for "setValidity()" in R 
is misleading here, especially the last four lines:

## Now we do something bad
t1 at x <- 1:20
## This should generate an error
## Not run: try(validObject(t1))

- Torsten