Le 16/08/13 19:33, Dirk Eddelbuettel a écrit :
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.
It was me who missed something actually. I though you called matteo() To my defense, I'm still recovering from an insolation.
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
-- 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 _______________________________________________ Rcpp-devel mailing list Rcpp-devel@lists.r-forge.r-project.org https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel