Re: [Rd] can R_do_slot_assign change slot type

From: Douglas Bates <>
Date: Tue 22 Mar 2005 - 13:09:30 GMT

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. mailing list Received on Wed Mar 23 00:46:09 2005

This archive was generated by hypermail 2.1.8 : Mon 24 Oct 2005 - 22:26:31 GMT