boostorg / preprocessor

Boost.org preprocessor module
http://boost.org/libs/preprocessor
96 stars 75 forks source link

Macro closures #29

Closed hirrolot closed 4 years ago

hirrolot commented 4 years ago

I've already implemented macro closures (anonymous macros). I can adapt them to Boost Preprocessor, are you interested in it?

eldiener commented 4 years ago

What are "macro closures" ?

hirrolot commented 4 years ago

Like usual closures, but behaving as macros, not functions. For example:

int main(void) {
    // Prints 16.
    int square = EXEC_MCLOSURE(MCLOSURE(ARG * ARG), 4);
    printf("%d\n", square);
}

Which is identical to:

#define SQUARE(number) ((number)*(number))

int main(void) {
    // Prints 16.
    int square = SQUARE(4);
    printf("%d\n", square);
}
eldiener commented 4 years ago

I am not familiar with the term "closure" in C++. Does it correspond to anything in that language ?

So a macro closure is some expresssion, with names which represent "variables" to be filled in when the macro enclosure is invoked, is that the idea ?

hirrolot commented 4 years ago

Yes, this is the idea. It corresponds to https://www.cprogramming.com/c++11/c++11-lambda-closures.html in C++.

eldiener commented 4 years ago

I am familiar with C++ lambdas but did not realize they were also called closures. I would be interested in adding your macro closures into Boost PP and/or Boost VMD, since the latter is purely variadic macros, while this library does still support compilers which do not implement variadic macros although there are few of these left.

I do need some good documentation about your macro closures, such as how many variables can be in the expression, what operators the expression supports, any limitations on the name of the variables in the expression, and such. Also some tests for macro closures need to be written up and added to the test framework. Finally some sort of documentation explaining why macro closures would be preferable to specific macros. But a PR for macro closures would certainly be welcome by me. I agree that it is an interesting idea and I give you full, credit for conceiving of it and implementing it. But a practical case for macro closures still needs to be made .

hirrolot commented 4 years ago

Thank you, I'll send a PR when I finish!

hirrolot commented 4 years ago

Apologize for the late response. I came up with this: https://gist.github.com/Hirrolot/c45af231a3c21af5448df4d5b422add3. However, such a design clearly obliterates the expressive power of closures, since almost any closure would consist of BOOST_PP_CLOSURE_ARG_*, BOOST_PP_CLOSURE_COMMA, BOOST_PP_CLOSURE_RPAREN and so on.

eldiener commented 4 years ago

I need an example how your macro closure is supposed to work. I assume a macro closure works with other macros, but I need to see how it is supposed to fit togeher. Documentation would also be welcome.