Want this course?
The key to understanding macros
Course: Beginning with Clojure Macros
The key to understanding macros is something that takes time to develop. You might as well start now! It has to do with separating, in you mind, the difference between runtime and compile time. This lesson explains it.
We're gonna be talking about Macros. Macros are one of these super exciting features of Lisp, everyone talks about them. Most people who talk about them might not even know what they are.
Macros can get complicated, and I want to talk about the key, the secret to understanding and writing them. And also I want to say, there's a lot of talk about, "oh you shouldn't use Macros, you should use functions, you should use data instead". Here's the thing, every tool has it's use and macros certainly, it applies to macros too, they have their use.
And the people who say that have gone through a process of understanding how macros work and they realize that they weren't the silver bullet that everyone talks about. But how do they understand that? They practice, they made lots of macros, they made lots of mistakes, they went through and they did it, they wrote hundreds of macros and then they realized "wow, this probably isn't what I thought it was".
So what I encourage you to do is go through that same process, have fun with macros. Do weird and interesting things with them. It's a controversial topic. One side is dominating at the moment, but there are some languages, some groups of people, some schools of thought, that use macros way more than is common. So really it's up to you, go play with them, make crazy macros, make very simple macros, do the whole spectrum, experiment and figure out what works best for you.
Okay so now we're ready to get into macros, because they are fun, they are mind bending. I'm going to bend your mind so that you can get it, okay?
So I have a little visual, so if you're this programmer and your computer is the turtle, right? This is sort of like logo. You're writing code to control this turtle. The one trick you have to do in your mind to do macros is that there's another turtle in the middle. Okay, so you want this turtle to move, but your code has to go through this turtle. And this turtle writes code to that turtle. Okay, so when you're writing macros, you're actually controlling this turtle who's outputting code for this turtle. So ultimately you are controlling this, because this turtle is well understood, it's not making stuff up. But you are controlling this turtle, writing code to control this turtle, and then this turtle is outputting code to this. Okay so we're gonna go over that in a little more detail. So you are programming, that's called development time. The time that you're coding and changing things and experimenting, that's development time. There's compile time which is when macros are expanded. Okay that's the process of taking the code with macros and turning it into code that this turtle can understand. It's called macro expansion. And then this is run time, or evaluation time. So this is when the code is actually run, it's actually evaluated, okay?
So what happens is the programmer turns coffee into code, that code has macros in it, potentially, probably, and so the macro expansion takes that code and turns it into code that this turtle can understand, no macros. And then that code, it runs it, and it does stuff.
Okay so a macro is just a function from code to code and the macro expansion's job is to make sure that the code that comes out of here can be run on this turtle. So the main trick is that you have to remember that there's two times happening here. The development time is easy, that's when you're typing, that's easy to think of. But normally we're used to just thinking about run time, like what is this gonna do at run time? You have to start thinking compile time and run time. There's two phases and macros happen at compile time. So in the code you have to distinguish in your mind, is this gonna be running when you're writing a macro, is this gonna be running at compile time or at run time?
And we'll see that, we'll see a lot of examples so you'll get that. But that's what you really have to focus on. Okay let's just get right into the code.