Nazari
Cryptic error injecting a function in a module with a Macro
Anyone can help me? Why compiler is throwing this error?:
** (ArgumentError) argument error
(stdlib 3.13) :lists.keyfind(:counter, 1, :SyncKey)
(elixir 1.10.4) src/elixir_utils.erl:45: :elixir_utils.var_context/2
(stdlib 3.13) lists.erl:1354: :lists.mapfoldl/3
Here is the source:
defmodule Problem do
def sample,
do: {
:foldercreate,
{:FolderHierarchy, :FolderCreate,
[
{:FolderHierarchy, :SyncKey, :reqres},
{:FolderHierarchy, :ParentId, :req},
{:FolderHierarchy, :DisplayName, :req},
{:FolderHierarchy, :Type, :req},
{:FolderHierarchy, :Status, :res},
{:FolderHierarchy, :ServerId, :res}
]}
}
defmacro res_cmd() do
{fnname, {namespace,container,lista}} = sample()
quote do
def unquote(fnname)(:res, pars) do
{
unquote(namespace),
unquote(container),
unquote(lista)
|> Enum.filter(fn {_, _, ty} -> ty == :res or ty == :reqres end)
|> Enum.map(fn {a,b,_c} -> {a,b,pars[b]} end)
}
end
end
end
end
defmodule Injected do
require Problem
Problem.res_cmd()
end
Most Liked
kip
Macros, like functions, have to be compiled and available before they can be called. Since macros are expanded at compile time, they need to be defined in a separate module and can’t be defined in the same module that calls them. Which is why your second example works. Its actually the same for functions. For example the following would also not work.
defmodule MyMod do
# won't be available to be called during module definition
def my_fun do
[1,2, 3]
end
# my_fun/0 isn't compiled and available here
# but this would work if `my_fun/0` was defined in another module
for i <- my_fun() do
def other_fun(unquote(i)), do: i
end
end
LostKobrakai
With defmacrop they can be in the same module, but need to be defined before being used only later in the same module‘s code.







