[Rcpp-devel] syntactic sugar

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


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 )

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 ...

-- 
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