您可以使用 Macro.expand/2 扩展宏
iex> Macro.expand((quote do: (if true, do: 1)), __ENV__)
{:case, [optimize_boolean: true],
[true,
[do: [{:->, [],
[[{:when, [],
[{:x, [counter: 6], Kernel},
{:in, [context: Kernel, import: Kernel],
[{:x, [counter: 6], Kernel}, [false, nil]]}]}], nil]},
{:->, [], [[{:_, [], Kernel}], 1]}]]]}
然后您可以使用Macro.to_string/2 将输出作为字符串而不是AST:
iex> Macro.expand((quote do: (if true, do: 1)), __ENV__) |> Macro.to_string()
"case(true) do\n x when x in [false, nil] ->\n nil\n _ ->\n 1\nend"
然后您可以使用IO.puts/2 将字符串打印到终端:
iex> Macro.expand((quote do: (if true, do: 1)), __ENV__) |> Macro.to_string() |> IO.puts()
case(true) do
x when x in [false, nil] ->
nil
_ ->
1
end