[Rcpp-devel] Default value for Rcpp List

Dirk Eddelbuettel edd at debian.org
Fri Aug 16 19:33:54 CEST 2013


Hi Romain,

On 16 August 2013 at 19:24, Romain Francois wrote:
| Le 14/08/13 16:25, Dirk Eddelbuettel a écrit :
| >
| >
| > On 14 August 2013 at 15:57, Matteo Fasiolo wrote:
| > |  thank you very much for your replies. In the end a named vector is
| > | enough for my purpose:
| > |
| > | cppFunction(
| > |   '
| > |   NumericVector myVett(NumericVector x = NumericVector::create(0) )
| > |  {
| > |   if(x.size() == 0) x = NumericVector::create(_["a"] = 1.0, _["b"] = 2.0);
| > |   return x;
| > |  }
| > |  '
| > | )
| >
| > You don't even need the assignment in the function definition:
| >
| > R> cppFunction("NumericVector matteo(NumericVector x) { if (x.size()==0) x = NumericVector(4); return(x); }")
| > R> matteo(vector())
| > [1] 0 0 0 0
| > R> matteo(1:3)
| > [1] 1 2 3
| > R>
| >
| > Dirk
| 
| This might not stay valid code when we put more control for dispatch, 
| argument recognition etc.
| 
| Some of that is in place with Rcpp::is, the rest will follow. We might 
| lose things like this, but we will gain more useful things instead.
| And we never said passing 0 arguments to a function needing one works, 
| so it is not a documented feature.

I am missing something here.  

I wrote a function with one argument, I supply one argument in each of the
two calls -- the argument just happens to be an empty vector precisiely
because I test for size in the function body.

I don't see anything fishy or questionable here.  

Dirk

-- 
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com


More information about the Rcpp-devel mailing list