num = 1
iex(17)> quote do: 1 + num
{:+, [context: Elixir, import: Kernel], [1, {:num, [], Elixir}]}
when will be the 1 will be assigned to num. is it evaluated at runtime or compile time?. I guess it’s evaluated at runtime otherwise we get 1,1 as arguments in AST. but I need clarity and more thing is assignment operator =
is macro?
KrupanandaRed10:
num = 1
iex(17)> quote do: 1 + num
{:+, [context: Elixir, import: Kernel], [1, {:num, , Elixir}]}
when will be the 1 will be assigned to num. is it evaluated at runtime or compile time?. I guess it’s evaluated at runtime otherwise we get 1,1 as arguments in AST. but I need clarity
quote
produces a piece of code represented as AST. The num
in the AST is not linked in any way to the num = 1
declared before it:
iex(1)> num = 1
iex(2)> ast = quote do: 1 + num
{:+, [context: Elixir, import: Kernel], [1, {:num, [], Elixir}]}
iex(3)> ast2 = quote do
...(3)> num = 2
...(3)> unquote(ast)
...(3)> end
iex(4)> {result, _binding} = Code.eval_quoted(ast2)
iex(5)> result
3
When building the AST you can use values from variables by using unquote
:
iex(1)> num = 1
iex(2)> ast = quote do: 1 + unquote(num)
{:+, [context: Elixir, import: Kernel], [1, 1]}
Similar how you can think of quote
/unquote
as here:
iex> ast = "1 + num"
iex> ast = "1 + #{num}"
Assignment is a special form and indeed it’s a macro.
2 Likes