Metaprogramming may feel daunting at first. We're doing a lot even before compilation starts, and the errors and warnings we get may seem cryptic sometimes. Having a clear end goal on your mind, and performing small iterations, will allow you to get comfortable with the quote/2
and unquote/1
dance and let you accomplish some neat macro-based features.
What follows is a brief compilation of the main concepts and tools we applied, which you can now incorporate in your tool belt:
- The abstract syntax tree of any expression, also called a quoted representation, is a nested structure of three element tuples that the compiler knows how to convert into BEAM bytecode. You can get this quoted representation by using the
quote/2
macro. - Inside a
quote/1
block, the compiler is generating the quoted representation of each statement. When it finds anunquote/1
, it stops the AST generation and evaluates and injects the value it gets. - The
bind_quoted
option of thequote/2
macro helps you to...