What Are Elixir Macros, The Most Powerful Elixir Feature — Tentamen Software Testing Blog

Image for post
Image for post
Elixir Macro Run in IEx


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. quote and 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.

Originally published at https://blog.tentamen.eu on October 28, 2020.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store