10

The first abuse that comes to my mind in C is:

#define if while 

But at the same time it is extremely handy and powerful when used correctly.

Something similar happens with Common Lisp macros.

Why doesn't all the programming languages support macros like this and what are the alternatives?

Is it they are considered harmful?

3

3 Answers 3

15

I am of the opinion that if a language has macros, they should be a well planned out and integral part of the language and not of the compiler.

Example, Lisp's macro system is a very powerful integrated language feature and is subject to all the rules and regulations of Lisp itself.

Counter-example, C/C++ macro system is separate from the language and built into the compiler. The result is that you are not limited to the constraints of your language and can create invalid code and redefine language specific keywords.

At the end of the day, there are several languages that have no macro feature--but those fatures are not missed as much. It all depends on how expressive a language is, and whether it has alternative approaches to meta-programming. Meta-programming is just a way of ensuring that when you do X, X is done the same way all throughout the application.

3
  • 4
    Worse, the C preprocessor is not even part of the compiler.
    – user7043
    Dec 21, 2010 at 17:04
  • 1
    What is the difference you are trying to draw here between "the language" and "the compiler"? C's macro system is defined in the language standard, Lisp's macros are, in fact, expanded by the Lisp compiler, and any language implementation is defined, when all is said and done, by the compiler and the standard libraries. Therefore, the phrase "separate from the language and built into the compiler" is nonsensical. Perhaps the distinction you're searching for is that C macros are implemented in the compiler front-end, and Lisp macros on the back-end? Aug 21, 2015 at 20:31
  • The distinction has to do with the consistency of the language. Think of it this way, you're planning on going to a foreign country and you have to learn to speak French to go places and buy food. When dealing with customs would you rather also have to learn Swedish or just deal with French. C precompiler macros are both syntactically and grammatically different than standard C. The cognitive challenge is then figuring out what that other language is going to do to your standard C program. In some cases it's easy, but I've seen whole code phrases as a macro definition. Now debug it. Aug 24, 2015 at 12:33
8

C macros and Lisp macros are entirely different. C macros are expanded using string substitution before any other processing is done. Lisp macros are expanded after the input text has been parsed into a syntax tree1, and can use the entire language during expansion. With Lisp macros, you can not only do stupid things like #define begin {, but you can define your own control structures, and even populate arrays at compile-time using whatever code you want.

One reason for not including macros is that anything more complicated than simple string substitution can be very hard to work with in languages with C-style syntax. Another complaint about macros is that they can make code more difficult to read, which can be true if not implemented skillfully. Well-written Lisp macros can actually make the code easier to read.

1except for read macros, which are expanded during the process of creating the syntax tree.

2

I don't think there is a specific reason why they aren't supported in some languages, same as why some are case sensitive and some aren't. No real reason usually, just a decision that was made.

BUT the reason they aren't included definitely isn't security. The

#define X Y

statement changes all X's to Y's at compile time. If you can change the #define statements, you could just copy/replace the source you were interested in changing and compile again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.