I’m looking at some Macro code that generated functions using __using__
– and it had resorted to Code.eval_quoted
to handle the keyword list parameters.
It looks like this was done because the code blows up when handling arguments of tuple size 3 without it (which does look AST-like).
Is it required to do that?
Here’s a snippet demonstrating the problem:
defmodule Options do
defmacro __using__(opts) do
IO.inspect(Keyword.get(opts, :cat), label: "compile time value")
quote do
def show() do
IO.inspect(unquote(opts))
end
end
end
end
defmodule Options.Normal do
use Options, cat: "meow"
end
defmodule Options.Quoted do
use Options, cat: [{"this", "ok"}]
end
defmodule Options.Broken do
use Options, cat: [{"¿", "?", :spanish_query}]
end
and the compiled output:
Compiling 1 file (.ex)
compile time value: "meow"
compile time value: [{"this", "ok"}]
compile time value: [{:{}, [line: 23], ["¿", "?", :spanish_query]}]
Note, the three tuple gets converted into AST syntax which subverts my expectations…
Besides avoiding macros, is there a best practice way of handling this?
Thanks!