I’m trying to pass some options to a module when I use
it, but anything beyond a simple list seems to get quoted (?) and arrives in abstract tree syntax (AST) format. Forgive the OO terminology, but here’s an example of the issue:
defmodule MyChild do
use MyParent, default_sort: %{updated_at: -1}
end
Then, in MyParent
:
defmodule MyParent do
defmacro __using__(opts) do
IO.inspect(opts)
# ...
end
end
The output of the inspection is something like this:
[
default_sort: {:%{}, [line: 20], [updated_at: {:-, [line: 20], [1]}]}
]
What’s going on there?
Specifically, I would like to create a module attribute in the __using__
block so that all of the functions provided in the __using__
block and any functions in the calling MyChild
module can reference it. But so far, the only solution I’ve found is to declare the module attribute before the use
statement, e.g.
defmodule MyChild do
@default_sort %{updated_at: -1}
use MyParent
end
That works, but it feels slightly weird to have the module attribute before the use
.
Thanks for any insights.