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

Aetherus
Hello, guys. I’m back again, but only for the weekends, maybe. This topic is about Day 13 of the Advent of Code 2020 . Thanks to @egze,...
New
adamu
I said I was on a break, but I took a sneak peak and it looked fun so… Part 1 completes in half a millisecond with a single pass of the ...
New
lud
At first I was scared but I found is a simple way to compute the sides. defmodule AdventOfCode.Solutions.Y24.Day12 do alias AdventOfCo...
New
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
bjorng
Here is my solution for day 1 of Advent of Code: defmodule Day01 do def part1(input) do all = parse(input) {first, second} = E...
New
bjorng
Note: This topic is to talk about Day 16 of the Advent of Code 2019. There is a private leaderboard for elixirforum members. You can joi...
New
code-shoily
Here’s my day 3 code This was quite easy. I was afraid Part 2 would be “un-regex-able” and was preparing for hand crafting automata bu...
New
Aetherus
Finished Day 1 with Elixir :tada: Here’s my code: #!/usr/bin/env elixir defmodule Combination do @doc "Yields each combination of 2...
New
mattbaker
I’m having so much fun working on the “Protohackers” challenges, I never got into Advent of Code much but this has been amazing. The chal...
New
rugyoga
Fairly straightforward Dijkstra’s algorithm import AOC aoc 2023, 17 do def compute(input, candidates) do {{max_row, max_col}, ite...
New

Other popular topics Top

greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31107 143
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

We're in Beta

About us Mission Statement