[英]Elixir quote vs escape
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]}
quote
会保持原点a,而Macro.escape
会在返回的AST中注入一个val。
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"]}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.