[Rcpp-devel] Default value for Rcpp List

Romain Francois romain at r-enthusiasts.com
Fri Aug 16 19:24:28 CEST 2013


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.

Romain


-- 
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30

R Graph Gallery: http://gallery.r-enthusiasts.com

blog:            http://blog.r-enthusiasts.com
|- http://bit.ly/13SrjxO : highlight 0.4.2
`- http://bit.ly/10X94UM : Mobile version of the graph gallery



More information about the Rcpp-devel mailing list