I have a module which provides some functionality, then another module that provides some other functionality on top of the first one.
I wrote this using nested use
, but the result is that one of the modules receive the options in an unexpected format and I need to “open” such format to provide some default values. The code is the following
defmodule Blah do
def hi do
IO.puts("hi")
end
end
defmodule Foo do
defmacro __using__(opts \\ []) do
IO.puts("Foo")
IO.inspect(opts)
IO.puts("Foo UNQUOTED")
# I'd like to convert the alias to "unquoted" here so that I can
# Keyword.put_new on `opts`
# IO.inspect(opts)
quote location: :keep do
IO.puts("Foo QUOTED")
IO.inspect(unquote(opts))
def hello do
nested_hello()
IO.puts("hello")
IO.inspect(unquote(opts))
end
end
end
end
defmodule Bar do
defmacro __using__(opts \\ []) do
IO.puts("Bar")
IO.inspect(opts)
quote location: :keep do
IO.puts("Bar QUOTED")
IO.inspect(unquote(opts))
use Foo, unquote(opts)
def nested_hello do
IO.puts("nested hello")
end
end
end
end
defmodule Test do
use Bar, name: Blah
end
Test.hello()
The output I get so far is:
$ elixir ./double_use.exs
Bar
[name: {:__aliases__, [line: 43, counter: -576460752303423480], [:Blah]}]
Foo
[name: {:__aliases__, [line: 43, counter: -576460752303423480], [:Blah]}]
Bar QUOTED
[name: Blah]
Foo QUOTED
[name: Blah]
nested hello
hello
[name: Blah]
So I’m quite lost in how to pass the options to the other module.