How to use quote and unquoted parameters inside a quote block?

I am building a macro witch has two parameters, one of them I want to unquote and the other one no.

Example:

  defmacro x(a, b) do
    quote do
      IO.inspect(unquote(a)) // should print 1 
      IO.inspect(b)// I want the ast [foo: {:^, [], [{:bar, [], Elixir}]}]
    end
  end
 
  x(1, [foo: ^bar])

I dunno if that’s all the content or there is a question missing. Are you asking how to do it? or there is something missing in the content?

The one you unquote you are already doing correctly.

To make sure you embed a quoted result into a quoted context, use Macro.escape/1.

So the snippet would become

quote do
  IO.inspect(unquote(a))
  IO.inspect(unquote(Macro.escape(b))
end
1 Like