quote返回代码块中传递的AST.
Macro.escape返回传入值的AST.
这是一个例子:
iex(1)> a = %{"apple": 12, "banana": 90}
%{apple: 12, banana: 90}
iex(2)> b = quote do: a
{:a, [], Elixir}
iex(3)> c = Macro.escape(a)
{:%{}, [], [apple: 12, banana: 90]}
Run Code Online (Sandbox Code Playgroud)
quote将保持原点a,同时Macro.escape将一个val注入返回的AST.
iex(4)> Macro.to_string(b) |> Code.eval_string
warning: variable "a" does not exist and is being expanded to "a()", please use parentheses to remove the ambiguity or change the variable name
nofile:1
iex(5)> Macro.to_string(c) |> Code.eval_string
{%{apple: 12, banana: 90}, []}
iex(6)> Macro.to_string(b) |> Code.eval_string([a: "testvalue"])
{"testvalue", [a: "testvalue"]}
Run Code Online (Sandbox Code Playgroud)