I’m trying to use one macro inside another macro and running into issues:
defmodule Test do
defmacro somemacro(arg1, do: block) do
quote do
defmodule unquote(arg1) do
def unquote(arg1), do: 1
unquote_splicing(elem(block, 2))
end
end
end
defmacro othermacro(arg2, arg1, do: block) do
__MODULE__.somemacro arg1, do: quote do
def unquote(arg2), do: 2
end
end
end
I get that I can’t unquote inside othermacro
so how do I do this?
Another try:
defmodule Test do
defmacro somemacro(arg1, do: block) do
quote do
defmodule unquote(arg1) do
def unquote(arg1), do: 1
unquote_splicing(elem(block, 2))
end
end
end
defmacro othermacro(arg2, arg1, do: block) do
newblock = quote do
def unquote(arg2), do: 2
end
__MODULE__.somemacro arg1, do: newblock
end
end
ERROR: you must require Test before invoking the macro Test.somemacro/2
requiring Test
within Test
causes another error:
(CompileError) you are trying to use the module AEnum which is currently being defined.
This may happen if you accidentally override the module you want to use. For example:
defmodule MyApp do
defmodule Supervisor do
use Supervisor
end
end
In the example above, the new Supervisor conflicts with Elixir's Supervisor. This may be fixed by using the fully qualified name in the definition:
defmodule MyApp.Supervisor do
use Supervisor
end
Basically othermacro is the same as somemacro except it adds some extra functions to the module
I am trying to use the do block to make a composition of the bits of the module … any help welcome