Famous (un)quotes

Let's now investigate what unquote does and see its role on the interaction of the quote and unquote dynamic duo. Until now we only injected quoted expressions on the caller module (recall the code block injected by the Demo.simple/1 macro injected on the Playground module).

As you will see, the unquote/1 macro will be extensively used, almost whenever we use a macro. The unquote/1 macro can only be used inside a quote block and lets us inject the value of a variable defined on an outer scope.

Let's continue with the previous Demo module, but this time we want the param variable to be used inside the quote block returned by the Demo.simple/1 macro:

iex> defmodule Demo do...>   defmacro simple(param) do...> IO.puts "Inside ...

Get Mastering Elixir now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.