3 ms·
Macros are for hiding common patterns in your code. Instead of typing out a given block of code mutatatis mutandis every time you need to use a slight variation
by wbhart 15y ago
Macros are for hiding common patterns in your code. Instead of typing out a given block of code mutatatis mutandis every time you need to use a slight variation of it, you code up a macro for it and just call the macro each time.
They work by not evaluating expressions but treating them as blocks of unevaluated code. The blocks of code can then be manipulated, transformed, split apart and reassembled all using the full power of the language. You aren't manipulating the values of those expressions, but the code itself.
The simplest example of a macro is a function which does not evaluate its arguments until you specifically tell it to do so. But they can be arbitrarily more complex than that.
Lisp is very flexible because it has no syntax to speak of and code is data. So manipulating the code is the same as manipulating data. With languages like Scala that have syntax, macros have to work with either strings representing the language elements or objects which represent them (expression trees as they are called in the post). In other words, they work with some abstract version of the syntax of the language.