For a very long time, I had a hard time trying to understand how macros work in Elixir. Since I don’t have any Lisp background, that’s a whole new concept for me.
But yesterday, after reading the relevant section in Elixir’s getting started guild, I suddenly realised how
unquote work. First of all, an Elixir expression is internally represented as a tuple with three elements, which is Elixir’s version of the Abstract Syntax Tree (AST).
quote can give you the AST representation of a piece of code, so that you can manipulate later.
unquote only works inside
quote and it can evaluate an expression and then inject the resulting value into the AST.
unquote are probably the most important building blocks of macros in Elixir.
Leave a comment