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

First Post!

Nazari

Nazari

This is getting strange… this version works; it defines a function foldercreate into module Injected as desired:

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()
      filtered = lista
        |> Enum.filter(fn {_, _, ty} -> ty == :res or ty == :reqres end)
        |> Enum.map(fn {a,b,_c} -> {a,b} end)
      quote do
          def unquote(fnname)(:res, pars) do
            {
              unquote(namespace),
              unquote(container),
              unquote(filtered)
                |> Enum.map(fn {a,b} -> { 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.

Last Post!

Nazari

Nazari

Hi kip; the fact is that the definition of the macro is in module Problem, and used in module Injected. The same behavior occurs it you split in two files each for a module. Also, the second version posted works, with no problem. I’ve reported this to elixir-lang, may be a bug (Compile error for function definition via macro · Issue #10308 · elixir-lang/elixir · GitHub)
Thanks for your reply

Where Next?

Popular in Challenges Top

sasajuric
Note by the Moderators: This topic is to talk about Day 5 of the Advent of Code. For general discussion about the Advent of Code 2018 an...
New
bjorng
Note: This topic is to talk about Day 4 of the Advent of Code 2019. There is a private leaderboard for elixirforum members. You can join...
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
Aetherus
This topic is about Day 4 of the Advent of Code 2020 . Thanks to @egze, we have a private leaderboard: https://adventofcode.com/2020/le...
New
bjorng
This topic is about Day 9 of the Advent of Code 2021 . We have a private leaderboard (shared with users of Erlang Forums): https://adve...
New
Aetherus
Today’s challenge is quite interesting. I ended up using Zipper to solve this problem. Maybe I overengineered quite a bit. The data stru...
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

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

We're in Beta

About us Mission Statement