From bates at stat.wisc.edu  Tue Mar 22 14:09:30 2005
From: bates at stat.wisc.edu (Douglas Bates)
Date: Tue Mar 22 14:10:32 2005
Subject: [Rd] can R_do_slot_assign change slot type
In-Reply-To: <C698D707214E6F4AB39AB7096C3DE5A58FF619@phost015.EVAFUNDS.intermedia.net>
References: <C698D707214E6F4AB39AB7096C3DE5A58FF619@phost015.EVAFUNDS.intermedia.net>
Message-ID: <4240190A.4010606@stat.wisc.edu>

Vadim Ogranovich wrote:
> Hi,
>  
> It seems that R_do_slot_assign can change the type of an S4 class slot.
> For example I have a class
>  
> setClass("ostream", representation(id = "integer", keepOpen =
> "logical"))
> 
> and inside a C-function I was able to assign a RAW vector to the id
> slot. Is this intentional? I remember reading somewhere that the slot
> type is guaranteed by R. Maybe this doesn't extend to C code?
>  
>  
> Now, in the above example I actually don't know the eventual type of the
> 'id' slot, it will be set up in some C code that intializes the class
> instance. What is the right way to define the representation in such a
> case?
>  
> Thanks,
> Vadim

I think it would be more accurate to say that the SET_SLOT macro in C 
(which is preferred to using direct calls to the R_do_slot_assign 
function) does not check the type of the value being assigned.  I 
imagine this is for efficiency - you don't really want to have that code 
looking up the definition of the class of the target every time it is 
called.

Yes, this does provide you with rope with which to hang yourself. 
That's what programming in C is all about.  ;-)  If you are concerned 
about validity of objects from an S4 class, create explicit checks using 
validObject in your R code.


[Rd] can R_do_slot_assign change slot type
Douglas Bates