[Rcpp-devel] Dispatching based on SEXPTYPE
Romain Francois
romain at r-enthusiasts.com
Thu Nov 22 16:42:29 CET 2012
Le 22/11/12 16:25, Hadley Wickham a écrit :
>> #define DISPATCH_METHOD(method, x) \
>> switch( TYPEOF(x) ){ \
>> case REALSXP: \
>> return method<REALSXP>(x); \
>> case INTSXP: \
>> return method<INTSXP>(x); \
>> case STRSXP: \
>> return method<STRSXP>(x); \
>> case LGLSXP: \
>> return method<LGLSXP>(x); \
>> default: \
>> Rf_error("Unsupported type"); \
>> return x; \
>> }
>
> Why do we need a macro here? Is it not possible to pass in method as a
> function? It seems like there must be a more elegant way to do
> dynamic dispatch.
>
> Hadley
Unfortunately, there is not. The issue is that we don't know the type of
x at compile type, so we have to do this switch dance and trigger
explicit instanciations of all method<> overloads.
There is no other way I know about.
Romain
--
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
R Graph Gallery: http://gallery.r-enthusiasts.com
`- http://bit.ly/SweN1Z : SuperStorm Sandy
blog: http://romainfrancois.blog.free.fr
|- http://bit.ly/RE6sYH : OOP with Rcpp modules
`- http://bit.ly/Thw7IK : Rcpp modules more flexible
More information about the Rcpp-devel
mailing list