I'm trying to dynamically define some functions within the __using__/1 macro of a module. These functions will have their names defined by the opts parameter provided to the __using__/1 macro. Something like this:
defmodule MyModule do
defmacro __using__(opts) do
names = Keyword.get(opts, :names)
Enum.each(names, fn(name) ->
quote bind_quoted: [name: name] do
def unquote(:"function_for_#{name}")(param) do
IO.puts("Hello from function_for_#{name}!")
IO.puts("With parameter: #{param}.")
end
end
end)
end
end
Then, this module will be use'd by another module, like this:
defmodule UserModule do
use MyModule, names: [:foo, :bar]
end
The behavior I would expect is:
iex> UserModule.function_for_foo(:hello_world)
> Hello from function_for_foo!
> With parameter: :hello_world.
iex> UserModule.function_for_bar(:hola_mundo)
> Hello from function_for_bar!
> With parameter: :hola_mundo.
But, instead, none of them are defined:
iex> UserModule.function_for_foo(:hello_world)
> ** (UndefinedFunctionError) function UserModule.function_for_foo/1 is undefined or private
I've read some related documents for the last couple of days like: the Kernel.use/2 documentation, the Module documentation on compile callbacks, an example provided in the Domain Specific Languages documentation, and a very related question here in SO, but I just can't seem to make this work.
Thanks in advance, any help will be highly appreciated!
__using__, then maybe my whole approach is wrong?