[Rcpp-devel] Checking for a promise
Gabor Grothendieck
ggrothendieck at gmail.com
Wed Dec 29 19:35:42 CET 2010
On Wed, Dec 29, 2010 at 12:51 PM, Romain Francois
<romain at r-enthusiasts.com> wrote:
> Le 25/12/10 02:10, Gabor Grothendieck a écrit :
>>
>> How do I fix this so it works? The code is intended to return TRUE or
>> FALSE according to whether the variable with name nm in environment
>> env is a promise or not. In the example code below it returns FALSE
>> but I would like it to return TRUE. Is the promise being forced
>> somewhere along the way? Some other problem?
>>
>> library(Rcpp)
>> f<- cxxfunction(signature(env="environment", nm = "character"),
>> body=' Environment e(env);
>> std::string s = as<std::string>(nm);
>> return wrap(TYPEOF (e[s]) == PROMSXP); ',
>> plugin="Rcpp")
>>
>> # create promise
>> delayedAssign("prom", 3)
>>
>> # want it to return TRUE but it returns FALSE
>> f(.GlobalEnv, "prom")
>
> Hello,
>
> Sorry for jumping late in this thread, xmas oblige.
>
> Environment::operator[] eventually calls Environment::get() which is
> implemented (in Environment.cpp) as follows:
>
> SEXP Environment::get( const std::string& name) const {
> SEXP res = Rf_findVarInFrame( m_sexp, Rf_install(name.c_str()) ) ;
>
> if( res == R_UnboundValue ) return R_NilValue ;
>
> /* We need to evaluate if it is a promise */
> if( TYPEOF(res) == PROMSXP){
> res = Rf_eval( res, m_sexp ) ;
> }
> return res ;
> }
>
> So the promise is explicitely forced.
>
>
> We could augment the interface of Environment so that it adds something that
> would not force the promise. Perhaps adding ', bool force = false' to get
> and find.
>
> We could also add a ctor to Rcpp::Promise that would grab a promise from an
> environment, something like:
>
> Promise::Promise( const Environment& env, std::string& name){
> SEXP res = Rf_findVarInFrame( m_sexp, Rf_install(name.c_str()) ) ;
> if( TYPEOF(res) != PROMSXP){ be unhappy ; }
> setSEXP( res ) ;
> }
>
> Promises is a beast I rarely use, so the interface is still in a "to be
> improved" state. Suggestions are much welcome, for example pseudo code
> chunks of the sort of things one would do with them.
>
Thanks. Note that in a subsequent post I did list the sorts of
operations that would be nice and also did provide some working code
-- that code was just intended to show how to check for promises
without forcing them and seemed to show that one must descend to a
lower level to do this. (That code was not intended to address the
question of what is a desirable interface.) R-Forge, hence the
rcpp-devel archives, seem to be down right now but you can find this
post here:
http://www.mail-archive.com/rcpp-devel@lists.r-forge.r-project.org/msg01287.html
--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com
More information about the Rcpp-devel
mailing list