Understanding the macro expansion process

By convention, every macro must return an expression. The process of taking one or more expressions and returning a new one is called macro expansion. Sometimes, it helps to see the expression being returned without actually running the code. We can use the @macroexpand macro for that purpose. Let's try to use it for the @hello macro that we defined earlier in this section:

There are several things to note from this output:

  • The i variable was renamed rather oddly: #67#i. This is done by the Julia compiler to ensure hygiene, which we will discuss later in this chapter. Macro hygiene is an important characteristic to keep in mind so that the code being generated does not conflict with other code.
  • A comment was inserted into the loop that contains source file and line number information. This is a useful part of the expression when a debugger is used.
  • The function call to println is bound to the one in the current environment, Main. This make sense because println is part of the Core package and is automatically brought into scope for every Julia program.

So when does macro expansion happen? Let's go over that next.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.12.36.30