2

I'm using macros and I want to pass a dynamic identifier to an Absinthe macro enum, wanting to generate different enums with a set list. Everything is inside a for comprehension.

I've read that Kernel.apply/3 does not work on macros.

  1. I've also tried:
   for name <- [:hello, :world] do
       enum  unquote(name) do
          value(:approved)
       end  
   end

Getting as a result:

** (ArgumentError) argument error
   :erlang.atom_to_binary({:unquote, [line: 36], [{:name, [line: 36], nil}]}, :utf8)
  1. I also tried without the unquote:
   for name <- [:hello, :world] do
      enum name do
        value(:approved)
      end
   end

And get:

** (ArgumentError) argument error
   :erlang.atom_to_binary({:name, [line: 36], nil}, :utf8)

It seems that I can't unquote anything that I pass as the identifier of the macro enum. Is it possible to do this?

1 Answers1

3

It is possible. The problem is enum assumes the first argument is an atom.

defmodule MacroHelper do

  defmacro enum_wrapper(names, do: block) do
    for name <- names do
      quote do
        enum unquote(name), do: unquote(block)
      end
    end
  end

end

defmodule AbsDemo do

  use Absinthe.Schema.Notation
  import MacroHelper

  enum_wrapper [:hello, :world] do
    value :approved
  end

end
Kabie
  • 10,489
  • 1
  • 38
  • 45