Summary
In this chapter, we started by learning what metaprogramming is and its pros and cons. We also learned that metaprogramming should be used carefully, as it makes code more complex, and outlined cases when it is okay to leverage metaprogramming.
We learned that in Elixir, metaprogramming revolves around three constructs – quoted literals, macros, and compile-time callbacks. We saw how quoted literals are Elixir’s representation of an AST, which facilitates most metaprogramming. We then learned how to use macros to inject behavior, using quoted literals in a module at compile time. We also learned that quoted literals and macros are evaluated hygienically, but var!/2
can be used to bypass the hygiene and define variables outside of the scope of a quote. We then took a look at compile-time callbacks, which are used to run tasks (or add behavior) by hooking into the compilation of a module.
We then proceeded to see how a DSL should be designed and that it should...