In this section, you will be introduced to macros, which are used to insert generated code into the programs. So, a macro is simply a block of code that can be compiled directly rather than the conventional method of constructing expression statements and using the eval()
function. The advantage of using macros is that a block of code that has to be hardcoded multiple times can be generated on-the-fly by creating macros for it.
Let's create a macro named
welcome
to printWelcome to Julia
:macro welcome() return :(println("Welcome to Julia")) end
This is how it would look when done in the REPL:
Now, let's check the macro you have created in the preceding step. Macros are represented by an
@
before their name. So, your macro would be represented by@welcome()
. It can be checked as follows:@welcome()
This is how it would look when printed in the REPL:
Now, let's include...