bigmemory icon indicating copy to clipboard operation
bigmemory copied to clipboard

Using macros instead of extensive dispatch functions.

Open privefl opened this issue 8 years ago • 8 comments

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?

privefl avatar Aug 10 '17 12:08 privefl

@nathan-russell Thoughts?

eddelbuettel avatar Aug 10 '17 12:08 eddelbuettel

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

eddelbuettel avatar Aug 10 '17 12:08 eddelbuettel

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).

privefl avatar Aug 10 '17 13:08 privefl

(That URL lacks a second L before 2478)

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

eddelbuettel avatar Aug 10 '17 14:08 eddelbuettel

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.

kaneplusplus avatar Aug 10 '17 15:08 kaneplusplus

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

privefl avatar Aug 10 '17 15:08 privefl

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

kaneplusplus avatar Aug 10 '17 17:08 kaneplusplus

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.

eddelbuettel avatar Aug 10 '17 17:08 eddelbuettel