From ripley at stats.ox.ac.uk  Fri Feb 22 08:45:32 2008
From: ripley at stats.ox.ac.uk (Prof Brian Ripley)
Date: Fri, 22 Feb 2008 07:45:32 +0000 (GMT)
Subject: [Rd] level of mutability for the type of a SEXP
In-Reply-To: <27d1e6020802190440p4f51b6bai53010d1dec33c3a@mail.gmail.com>
References: <27d1e6020802190440p4f51b6bai53010d1dec33c3a@mail.gmail.com>
Message-ID: <alpine.LFD.1.00.0802191244250.13928@gannet.stats.ox.ac.uk>

On Tue, 19 Feb 2008, Laurent Gautier wrote:

> Dear list,
>
> I am writing C code to interface with R, and I would like to know the
> level of mutability for the type of a SEXP.
>
> I see that there is a macro/function TYPEOF(), and that it can be used
> as an l-value, as well as a macro/function SET_TYPEOF().

The macros are not available to user code, so TYPEOF is not available to 
use as an l-value.

> My question is "should the type be considered immutable, or it can it
> change after the SEXP has been created and used for a while ?".
>
> I understand that a call to TYPEOF will not perform any conversion,
> and my question is only about being sure that once the type of a given
> SEXP is set it cannot change.

It is SEXPRECs that have types, and their type can be changed and in a few 
cases is in R's internal code.  You should only change type between 
SEXPREC types with the same structure, e.g. from VECSXP to EXPRSXP or 
DOTSXP to LISTSXP.  I am unaware of any examples where this happens that 
involve a SEXPREC pointed to by a SEXP not in the internal code concerned.

Since a SEXP is a pointer, the type of the SEXPREC it points to can change 
(as which SEXPREC it is can) and frequently does -- that is one way 
arguments are coerced.

The R Internals manual has the details of the SEXPRECs and their 
structure.

I don't see the value of knowing though.  Anyone who needs to ask about R 
internals should regard the arguments passed to .Call/.External calls as 
read-only and also assume that there is no simultaneous evaluation going 
on that might change them.

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



[Rd] level of mutability for the type of a SEXP
Prof Brian Ripley