Hey everybody,
A quick question where I feel I am missing something obvious. Suppose we have this:
iex> code1 = quote(do: to_string('abc'))
{:to_string, [context: Elixir, import: Kernel], ['abc']}
iex> code2 = Code.string_to_quoted!("to_string('abc')")
{:to_string, [line: 1], ['abc']}
iex> Code.eval_quoted(code1)
{"abc", []}
iex> Code.eval_quoted(code2)
{"abc", []}
My question is: are code1
and code2
considered identical in terms of AST?
1 Like
They have different ASTs code1 != code2
do they are not identical in terms of AST. The metadata is different. In your example it happend to produce the same result but it doesn’t have to be that way:
iex1> 0 ↵
alias Alphabet, as: Abc
Alphabet
iex2> 0 ↵
code1 = quote do: Abc
{:__aliases__, [alias: Alphabet], [:Abc]}
iex3> 0 ↵
code2 = Code.string_to_quoted!("Abc")
{:__aliases__, [counter: 0, line: 1], [:Abc]}
iex4> 0 ↵
Code.eval_quoted(code1)
{Alphabet, []}
iex5> 0 ↵
Code.eval_quoted(code2)
{Abc, []}
iex6> 0 ↵
5 Likes