[Rd] can R_do_slot_assign change slot type

Douglas Bates bates at stat.wisc.edu
Tue Mar 22 14:09:30 CET 2005

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 

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.

More information about the R-devel mailing list