4 ms·
The main problem with C macros is their usage when a regular function would do just fine. That goes for a lot of the cruft in the Windows headers. Having said
by jstimpfle 7y ago
The main problem with C macros is their usage when a regular function would do just fine. That goes for a lot of the cruft in the Windows headers.
Having said that, while I object to most ideas that "C is missing", I do think that better constant expressions and expression macros (functions on the syntactic-expressions level) would be a usability improvement in APIs compared to (token stream) preprocessor macros. Even though in the presence of the preprocessor, constants and expression macros would be somewhat redundant.
One advantage of expression macros is that you do macro expansion after parsing to an AST. You get a valid AST parse with macro expansion if and only if you get a valid parse without expansion. Expression macros are regular AST elements, and are scoped just like regular functions or variables. They don't have a weird textual scoping scheme with conditional compilation or un-/redefinability. Getting rid of that should make it easier to translate them to other languages.
Another advantage is that they allow you to actually know which constructs in an interface are part of the API. An expression macro is always supposed to be used directly by the consumer of the API, or at indirectly through another macro that is. That particular problem of the C preprocessor could in fact be solved if headers cleaned up after themselves, but in practice, preprocessor is just to messy so nobody does that.
- youdontknowtho 7y agoIsn't this what rustlang did? I have to admit that I'm coming around on rust. I still think that their fanboy marketing is obnoxious...but I need to get over my penchant for finding reasons to be angry.
- jstimpfle 7y agoI think they have a variety of different kinds of them. I'm not sure I want to go there. There are already benefits to be had by something that looks basically like function-like macros in C, but where the replacement bodies are required to be valid expressions, and that is scoped like other functions and variables, and only expanded after parsing. I did that for my own (experimental) language and it was very easy to implement and use.
- steveklabnik 7y agoWe have two forms of macros: one that’s basically fancy pattern matching, and another that lets you manipulate an arbitrary series of tokens.
- wruza 7y ago>actually know which constructs in an interface are part of the API Yes, this. I could even tolerate cpp as it is, if I had this metadata. It is relatively easy to generate wrapper.c that simply re-invokes all interfaces, converting them to homogeneous ffi-able form. Like Pascal’s .int files which described everything you want to know as a unit user.