Kernel.SpecialForms.unquote
You're seeing just the macro
unquote
, go back to Kernel.SpecialForms module for more information.
Unquotes the given expression inside a quoted expression.
This function expects a valid Elixir AST, also known as
quoted expression, as argument. If you would like to unquote
any value, such as a map or a four-element tuple, you should
call Macro.escape/1
before unquoting.
Examples
Imagine the situation you have a quoted expression and you want to inject it inside some quote. The first attempt would be:
value =
quote do
13
end
quote do
sum(1, value, 3)
end
Which would then return:
{:sum, [], [1, {:value, [], Elixir}, 3]}
Which is not the expected result. For this, we use unquote
:
iex> value =
...> quote do
...> 13
...> end
iex> quote do
...> sum(1, unquote(value), 3)
...> end
{:sum, [], [1, 13, 3]}
If you want to unquote a value that is not a quoted expression,
such as a map, you need to call Macro.escape/1
before:
iex> value = %{foo: :bar}
iex> quote do
...> process_map(unquote(Macro.escape(value)))
...> end
{:process_map, [], [{:%{}, [], [foo: :bar]}]}
If you forget to escape it, Elixir will raise an error when compiling the code.