Hi,
This is an old thread but couldn’t any recent discussion in this direction so posting my question as it is most relevant here.
I was wondering if @homologist 's solution can be taken yet a step further.
I learned here that it is possible to do so with and without macro:
defmodule My do
@methods ~w|one two three|a
for method <- @methods do
def unquote(method)() do
IO.puts unquote(method)
end
end
end
defmodule MyMacros do
defmacro gen_funs(names) do
for name <- names do
quote do
def unquote(name)() do
IO.puts unquote(name)
end
end
end
end
end
defmodule My2 do
require MyMacros
MyMacros.gen_funs([:one, :two, :three])
end
Now, one step further, what about variable number of parameters?
I came as far as
defmodule MyMacros do
defmacro gen_fun(name, args) do
{
:def,
[context: Elixir, import: Kernel],
[
{
name, [context: Elixir],
for(arg <- args, do: {arg, [], Elixir})
},
[do: :ok]
]
}
end
end
defmodule My2 do
require MyMacros
MyMacros.gen_fun(:abc, [:a, :b])
end
This works but I can’t call MyMacros.gen_fun
in a loop iterating over a list function spec e.g.
for {api, args} <- [api1: [:a, :b], api1 : [:a], ...] do: MyMacros.gen_fun(api, args)
I am vaguely getting that this could be a quote
/ unquote
thing (or I am completely wrong).
Is it at all possible, what I am trying to achieve?
This is a pure academic question. I also understand that a solution (if exists) may not be a good practice and / or recommended, just curious.
Please go easy on me quite newbie with elixir 
TIA for satisfying my curiosity!