繁体   English   中英

Elixir引用vs逃脱

[英]Elixir quote vs escape

在Elixir中,如果使用Macro.escape/1而不是quote/1 我看过初学者指南并没有帮助。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM