[Rcpp-devel] syntactic sugar

Romain Francois romain at r-enthusiasts.com
Mon Mar 1 14:20:33 CET 2010


On 03/01/2010 02:04 PM, Romain Francois wrote:
>
> On 03/01/2010 01:48 PM, Dirk Eddelbuettel wrote:
>>
>> On 1 March 2010 at 12:20, Romain Francois wrote:
>> | Hello,
>> |
>> | I'm starting to find the Named construct somewhat too verbose and I'd
>> | like to add some syntactic sugar.
>> |
>> | I've commited something just now. I'd like to ear opinions about it.
>> |
>> | The machinery is built around the classes
>> | Rcpp::internal::NamedPlaceHolder, Rcpp::internal::NamedPlaceholderProxy
>> | and the Rcpp::_ object which is a static instance of NamedPlaceHolder.
>> |
>> | Here is an example :
>> |
>> | require( inline )
>> | require( Rcpp )
>> | funx<- cfunction(signature(), '
>> | 	Language call( "rnorm", _["mean"] = 10 ) ;
>> | 	return call ;
>> | ', Rcpp = TRUE, includes = "using namespace Rcpp;"  )
>> | funx()
>> |
>> |
>> | The interesting thing is
>> |
>> |   _["mean"] = 10
>> |
>> | which is the same as Named( "mean", 10 )
>>
>> On purely stylistic grounds, I dislike it.
>>
>> | There is also the ARG macro in Rcpp.h
>> |
>> | #define ARG(name) ::Rcpp::internal::NamedPlaceholderProxy name(#name) ;
>> |
>> | which allows the syntax :
>> |
>> | funx<- cfunction(signature(), '
>> | 	ARG(mean)
>> | 	Language call( "rnorm", mean = 10 ) ;
>> | 	return call ;
>> | ', Rcpp = TRUE, includes = "using namespace Rcpp;"  )
>> |
>> |
>> | There is the annoying bit of calling the macro, but I suppose an
>> | application could have calls to ARG in some header.
>> |
>> | Any ideas ? Suggestions ...
>>
>> If you feel must give users this rope, the let's do it.
>>
>> I mostly try to follow the rule of 'no #defines in C++'. Ie no macros.
>> Typedefs, sure.  The ARG one is useful so we could make it available but I
>> would not use it.  There is, it seems based on a quick glance, a way to use
>> this as
>>
>>         typedef ::Rcpp::internal::NamedPlaceholderProxy rcppProxy;
>>
>> and that would be good enough for me.
>>
>> But heck, there are only two of us voting here so if it is a tie and you
>> really want it, go for it.
>>
>> Dirk
>
> We can do :
>
> Language call( "rnorm", Named("mean") = 10 ) ;
>
> which I find slightly less annoying than ;
>
> Language call( "rnorm", Named("mean", 10) ) ;
>
> Maybe I can remove the _ but leave the classes so that the user can do :
>
> NamedPlaceHolder args ;
> Language call( "rnorm", args["mean"] = 10 ) ;
>
> The ARG macro is for the lazy ones who do not want to type the name of
> the argument twice.
>
> NamedPlaceholderProxy mean("mean") ;
>
> but then maybe ARG is not enough encapsulated a name, maybe something
> like RCPP_REGISTER_ARGUMENT or something, dunno.
>
> Anyone else out there with opinions ?
>
> Romain

What if I use some other namespace to host "_"

something like :

namespace Rcpp{
namespace placeholders{
static internal::NamedPlaceHolder _ ;
}
}

so that _ would get available upon

using namespace Rcpp::placeholders ;

We don't have to have it. Maybe my mind has been twisted by looking at 
code from Boost.Lambda, Boost.Spirit, etc ...


-- 
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://tr.im/OIXN : raster images and RImageJ
|- http://tr.im/OcQe : Rcpp 0.7.7
`- http://tr.im/O1wO : highlight 0.1-5



More information about the Rcpp-devel mailing list