Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
18  
Knives are dangerous. An alternative is to try cutting things with spoons. –  Matt Ellen Dec 20 '10 at 20:32
1  
See also: programmers.stackexchange.com/questions/937/… –  Shog9 Dec 20 '10 at 21:28
1  
@Matt or to provide good handles. –  OscarRyz Dec 20 '10 at 22:16
add comment

3 Answers

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.

share|improve this answer
add comment

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.

share|improve this answer
3  
Worse, the C preprocessor is not even part of the compiler. –  delnan Dec 21 '10 at 17:04
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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