4 ms·
Anytime macros are used for metaprogramming, it's time to reach for a more powerful language.
by WalterBright 5y ago
Anytime macros are used for metaprogramming, it's time to reach for a more powerful language.
- 10000truths 5y agoMacros are useful, as long as they're used sparingly. I think that in this case, it's used well - the struct is still perfectly readable, and the sole purpose of it is to make it so that you don't have to manually name the dummy fields. But you could totally just write out dummy1, dummy2, dummy3 etc. yourself if you want to get rid of the macros.
- WalterBright 5y ago> Macros are useful, as long as they're used sparingly. Everybody says that. Everybody believes it. And everybody goes to town making a rat's nest with macros, just like that snarl of cables under my desk that resist all attempts to make it nice. Myself included. I've even written an article about clever C macros. Look, ma! I was so proud of myself. But then I got older. I started replacing the macros in my C code with regular code. It turns out they weren't that necessary at all. I liked the C code a lot better when it didn't have a single # in it other than #include.
- Spivak 5y agoDoesn’t work in a lot of cases unfortunately. If you’re writing a library designed to be consumed by other languages you’re stuck with writing C abi compatible code which can be written in other languages that can “extern” them but it puts limits on what’s possible in those libraries.
- WalterBright 5y agoOne thing I do is to make my own interface to the 3rd party library with the ugly interface. Then that interface file is only one that sees it. It also may be true that one can't replace every use of the preprocessor. That shouldn't stop replacing what one can.
- throwaway17_17 5y agoI want to be clear about your meaning, because I don’t know if I’m reading your comment correctly. Are you referring explicitly to syntax based, preprocessor macros? Or does your comment extend to other metaprogramming techniques? I am inclined to think you mean the first considering the amount of emphasis on generic programming in D? Just curious.
- WalterBright 5y agoI'm referring to both syntax based (AST) macros, and text based (preprocessor) macros. The latter, of course, are much worse. An example of the former is so-called "expression templates" in C++. I've seen them used to create a regular expression language using C++ expression templates. The author was quite proud of them, and indeed they were very clever. However nice the execution, the concept was terrible. There was no way to visually tell that some ordinary code was actually doing regular expressions. C++ expression templates had their day in the sun, but fortunately they seem to have been thrown onto the trash pile of sounds-like-a-good-idea-but-oops. (I wrote an article showing how to do expression templates in D, mainly to answer criticisms that D couldn't do it, not because it was a good idea.)
- shaklee3 5y agoCan you link to it? We're using expression templates on a new library and I find it useful.
- WalterBright 5y agoOh man, you sure know how to drive a stake in my heart! What have I done! Some curses should just not be uttered. I'm not sure where it is on my backups. But it was done just like you'd do it in C++. The same thing, just with D syntax.
- shaklee3 5y agoWe're making a numerical computing library that expression templates are used for compile-time evaluation of expressions. It's a very niche hpc library, so it might be one of the few places it's appropriate.
- cryptonector 5y agoYou might as well have written that "any time you're reaching for C, it's time to reach for a more powerful language". But if -sadly- you must use C, metaprogramming using macros is not a terrible thing.
- WalterBright 5y ago> is not a terrible thing It is a terrible thing. It's possible to do without them, and you'll like your code better. Your symbolic debugger and syntax directed editor will work properly. The poor schlub who has to fix the bugs in your code after you leave will be grateful. Your spouse will be happy and your children will prosper. For example, #define foo(x) ((x) + 1) replace with: int foo(int x) { return x + 1; } The compiler will inline it for you. There's no penalty. #define FOO 3 Replace with: enum { FOO = 3 }; or: const int FOO = 3; Replace: #if FOO == 3 bar(); #endif with: if (FOO == 3) bar(); The optimizer will delete bar() if FOO is a manifest constant that is not 3.
- zerr 5y agoAgree except the last example - the clarity of intent that you want a conditional compilation is lost. Also, non-optimized debug builds are affected.
- CamperBob2 5y agoI think it was RMS who argued that conditional compilation should be considered harmful in general. Code that you put in an inactive #if(def) block will not be maintained, and is basically guaranteed to rot. If it's needed in the future, it'll likely have to be rewritten from scratch. According to this stance, any code that's suppressed by the C preprocessor should either be written in an if {} statement so that it will at least continue to compile as the surrounding code changes, or be replaced with comments describing what it does (or did), if it's important enough to keep track of. Can't really think of many good counterarguments to this. Machine dependence might be one, but then you could argue that the preprocessor is being used to cover up for an inadequate HAL.
- tpoacher 5y agoI hear your quote, but it's only a quote. A macro is effectively preprocessing facilitated by the language. You could always preprosess externally if you wanted, and there's nothing stopping you from doing that in the "Powerful Language (TM)" either. Now whether people use macros and preprocessing usefully is another question, but not one to which the answer is "abolish macros for more language features". When used correctly, macros ARE power.