In the previous post, we presented Elixir operators, where many of them are Elixir Functions. Today we present the most powerful Elixir Feature, Macros. This post is part of the functional language series, and it is based on the remarkable book Elixir In Action by Sasa Juric.
Transformations On Compile Time
Elixir Macro is a function that on compile-time inputs transforms into Elixir code. This is what Computer Virus does but in program runtime. Let’s present Elixir Macros as the example of an if statement. If statement is not a core part of Elixir language. It is Elixir Macro.
defmacro is Elixir’s keyword for Macro definition. Macro also belongs to Module.
unquote are core Elixir metaprogramming features. More about that could be read in Metaprogramming Elixir.
In the above screenshot, you can see how to use our
If Macro. It is mandatory to Import it. Otherwise, you will get a very cryptic error. You can try what happens when you use
:false as the first argument.