Nazari

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

kip

ex_cldr Core Team

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

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.

Where Next?

Popular in Challenges Top

ehayun
I have 2 arrays: a1 can be any combination of value or nil like that a1 = [1,nil,3] and array 2 the same a2 = [4,2, nil] How do I com...
New
adamu
Nobody’s doing Advent of Code this year? :smile: I might do the first week or so. For Day 1, first I solved it using regular expression...
New
sneako
Note by the Moderators: This topic is to talk about the first day of the Advent of Code. For general discussion about the Advent of Code...
New
New
NobbZ
Note by the Moderators: This topic is to talk about the Day 2 of the Advent of Code. For general discussion about the Advent of Code 201...
New
bjorng
Note: This topic is to talk about Day 3 of the Advent of Code 2019 . There is a private leaderboard for elixirforum members. You can jo...
New
bjorng
Note: This topic is to talk about Day 23 of the Advent of Code. For general discussion about the Advent of Code 2018 and links to topics...
New
christhekeele
Setting this down for the night, as after a quick naive solve for quick part 1 I realize that part 2 is by design computationally expensi...
New
Aetherus
Don’t know why the regex ~r/[\W && [^\.]]/x does not work in Elixir. It works pretty well in Ruby. Anyway, here is my solution:
New
christhekeele
Thought I’d kick today’s thread off! Parsing Enum rocks, so most of my code was actually in parsing input. ▶ Preprocessing input Part 1...
New

Other popular topics Top

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New

We're in Beta

About us Mission Statement