6 ms·
Can someone please explain macros, in an easy to grasp way?
by devdad 9y ago
Can someone please explain macros, in an easy to grasp way?
- sscarduzio 9y agoMacros are a tool given to the user by PL designers to extend the language with new syntax. I.e. a macro program is a piece of code that defines new keywords, operators, etc. and their implementation.
- coltonv 9y agoSay you're creating a programming language and you know everyone is going to love it and use it. You add some nice features like if statements, switch statements, and for loops which allow the people that use your language to decide when certain code runs and shortens the syntax for common patterns. Five years down the line your language is losing popularity fast, because all the hip new languages have an "unless" statement which is the opposite of an if statement. They have for-each loops, cond statements, and their if/else statements return values, so you can write less code to do those common patterns in those new languages than your language. You rewind time 5 years using a time machine and this time, when you create your langauge, you add all those cool features, only to realize in another 10 years your language has lost popularity because there's a new set features they want, lambdas, argument piping, etc. So this time when you go back in time, you add macros to your language. Macros are bits of code that allow people using your language to write code that produces more code. Now, you don't even have to write an unless statement, you can let the community write one for you and release it as a package. Anyone at any time can effectively add features to the language in their own project. That sounds fantastic, and it often is, but macros need to not be abused. If a developer writes a bunch of code using lots of his own macros in your language, anyone reading that code later is going to be very confused seeing that many language features that aren't in the language docs, because that random developer wrote them himself. Functions are much more explicit than macros, so if you can write whatever you're trying to write as a function instead of a macro, you'll be better off. For every success story of a language becoming easier to use because of macros, there's another story of a different language becoming frighteningly complicated due to macro abuse.
- flavio81 9y agoA "macro", in the sense we are using (i.e. not "C preprocessor macro" or "Excel macro"), is basically a function or procedure that "spits out" (returns) source code. It is writen in the programming language you use, and outputs source in the programming language you use. In other words, a macro is a program that writes programs. In languages that make heavy use of macros (like Lisp), a macro is compiled almost transparently, so you can use it as if it were a function. It behaves like a function but can do a lot more. One simple use is to eliminate having to write repetitive or "boilerplate" code, since you can just program a macro to write generate code for you. This generation occurs on the fly and transparently, so the code is more maintainable as well.
- retrogradeorbit 9y agoMacros run some code at compile time, that takes raw unevaluated source code as arguments, and returns new source code to replace it in the code. Then use this returned code instead in the compilation.
- devdad 9y agoThanks for all your great answers! I've learnt something new.