kaneplusplus / bigmemory

126 stars 24 forks source link

Using macros instead of extensive dispatch functions. #71

Open privefl opened 7 years ago

privefl commented 7 years ago

I've just come across the Cleaner Generic Functions with RCPP_RETURN Macros which are implemented there.

I think, with some tweaking, we could use macros for MatrixAccessors and types raw, char, short, int, float, double, complex in order to factorize code by using macros instead of extensive dispatch functions.

Yet, I don't know anything about C++ macros.

@eddelbuettel @nathan-russell Could this be feasible?

eddelbuettel commented 7 years ago

@nathan-russell Thoughts?

eddelbuettel commented 7 years ago

@privefl Can you remind us which source files of bigmemory you have in mind?

privefl commented 7 years ago

All of them, there are a dispatch function for half of the functions in src/ (e.g. https://github.com/kaneplusplus/bigmemory/blob/master/src/bigmemory.cpp#L2415-L2478).

eddelbuettel commented 7 years ago

(That URL lacks a second L before 2478)

Seems fine to me. <shrugs> But nothing wrong occasional refactoring.

kaneplusplus commented 7 years ago

It would certainly tighten the code up. Would you mind showing some example code for how it would look? I originally thought about a macro because there is a lot of repetitive code. However, I went with the current version because a) I thought it was more explicit and b) I didn't have all of the nice Rcpp functionality available now.

privefl commented 7 years ago

In fact, macros are quite easy to use. See an example on my package bigstatsr: https://github.com/privefl/bigstatsr/commit/4ac9b5265970ba140685bea0c8c3d21bc0ec1d27

kaneplusplus commented 7 years ago

Oh, OK. For some reason, I thought we were talking about something fancier (and harder to read). Sure, this looks fine to me.

eddelbuettel commented 7 years ago

I am just throwing this out here as personal preference which is not a swipe at the nice write-up.

MACROS are still evil. Sometimes we need them (as eg our beloved SEXP are poor man's OO via unions) but I still think that compiler-driven type safety is preferable. But in the end it all depends on the fine details.