Seeking clarification on unquote

Hi :wave:, I need clarification on how unquote works, I understand it’s needed to inject variables outside the quote context into the quote context, with string interpolation being used to hammer home this point.

Consider below simple macro.

defmacro clarify(expr) do
  quote do

A macro receives an AST and returns an AST; this means the AST of expr will be passed to the macro
Assuming 1 + 2 - 3 is pass, the AST will be

{:+, [context: Elixir, import: Kernel],
 [{:+, [context: Elixir, import: Kernel], [1, 2]}, 3]}
  1. Will unquote convert/evaluate this AST to 1 + 2 - 3
  2. Or will it evaluate the expression to 0
  3. Both
  4. Will evaluate this AST to 1+ 2 - 3, then the quote macro will convert it back to the AST which is returned and executed/evaluated.

Thank you.

1 Like

As you rightly say, its AST all the way down. Which means that unquoting a quoted expression inside a quote block will insert the expanded expression, which being inside a quote block will then be quoted again.


Thanks for swift answer, so in this context what will be the expanded expression for unquote(expr); will it be 1 + 2 - 3 or 0?

It will be the former (on mobile device). Expansion is just expansion, not evaluation. (Sorry for the brevity, on my phone)