Macro syntax sugar or different expressions?

Just a quick question regarding the Access modules AST formats.

Is

{{:., [], [{:__aliases__, [], [:Access]}, :get]}, [], [{:foo, [], Elixir}, :bar]}

the same as

{{:., [], [Access, :get]}, [], [{:foo, [], Elixir}, :bar]}

They seem to have different outputs with Macro.to_string, with the latter taking on the shorthand form of foo[:bar] and the former taking on the form Access.get(foo, :bar).

Yet

{{:., [], [{:__aliases__, [], [:Access]}, :get]}, [], [{:foo, [], Elixir}, :bar, :baz ]}

has the same output as…

{{:., [], [Access, :get]}, [], [{:foo, [], Elixir}, :bar, :baz]}
1 Like

It depends.

If your code does alias SomeWeirdModule, as: Access, which would arguably be a bad idea, then they will expand to different things, but in most cases the alias will expand to the module Access. This is exactly why Elixir emits {:., [], [Access, :get]} when you use the opts[key] syntax, so it is not dependent on aliases.

1 Like

Think I understand what you’re saying.

When the Access alias is the access module {:., [], [Access, :get]} is what you get, but when the Access alias is (or may be) something else the emitter doesn’t know if it is referring to another module so it just outputs the alias.