Today, we have
Macro.expand and
Macro.expand_once, but we only have
Macro.escape. I've recently found myself in need for something like
Macro.escape_once.
Say you want to build a map to be pattern matched agains, so that the result of the macro is the following code:
%{foo: foo, bar: bar} = something
If I do something like this to build the map:
for var <- [:foo, :bar], into: %{} do
{var, Macro.var(var, __MODULE__)
end
#=> %{foo: {:foo, [], Elixir}, bar: {:bar, [], Elixir}}
and then escape it in order to insert it into a quote, it becomes
{:%{}, [], [foo: {:{}, ...}, ...]}
since the variables {:foo, [], Elixir} and {:bar, [], Elixir} will be escaped (recursively). In order to solve this, I have to manually do:
kv = for var <- [:foo, :bar] do
{var, Macro.var(var, __MODULE__)
end
{:%{}, [], kv}
It's very very possible that I'm missing something here, so I apologise in advance for that :). However, in the remote case I'm not missing anything, what do you think about adding a Macro.escape_once function?