It seems that Macro.expand/2
cannot expand macros within special forms, for example,
iex(1)> print_expand = fn expr -> expr |> Macro.expand(__ENV__) |> Macro.to_string() |> IO.puts() end
iex(2)> print_expand.(quote do if 1 do :ok end end)
case(1) do
x when Kernel.in(x, [false, nil]) ->
nil
_ ->
:ok
end
iex(3)> print_expand.(quote do fn -> if 1 do :ok end end end)
fn -> if(1) do
:ok
end end
Above showed that Macro.expand/2
cannot expand if
macro within fn
special form.
Question: how to expand macros fully / recursively even it’s within special forms ?
One way I think maybe work is to walk through AST and execute Macro.expand/2
for all encountered special form nodes. But I would like to know if there is a better way ?