halostatue

halostatue

Macros not seeing passed-in attributes

How do I make this work in a defenum macro?

  defenum EmailStatus, :email_status, @email_states

Right now, I’m getting:

warning: undefined module attribute @email_states, please remove access to @email_states or explicitly set it before access

It’s making no difference whether I’m using Macro.expand/2 or not:

values: {:@, [line: 122], [{:email_states, [line: 122], nil}]}
values-expanded: {{:., [],
  [
    {:__aliases__, [counter: {MyApp.Enum, 9}, alias: false],
     [:Module]},
    :get_attribute
  ]}, [],
 [
   {:__MODULE__, [counter: {MyApp.Enum, 9}], Kernel},
   :email_states,
   122
 ]}

I actually want to pass in a value that’s closer to:

  @a ~w(a b c d)
  @b ~w(e f g h)
  @c ~w(I j k l)
  defenum EmailStatus, :email_status, @a ++ @b ++ @c

The macro in question is complex, and may eventually be released as a fork of an already existing package. But for the purposes of this test, I believe that this will do:

  defmacro defenum(module, type, values) do
    quote location: :keep do
      defmodule unquote(module) do
        @enum_type unquote(type)
        @values Enum.map(unquote(values), &to_string/1)

        def type, do: @enum_type
        def values, do: @values
      end
    end
  end

Ideas?

-a

Marked As Solved

Eiji

Eiji

@halostatue First of all I do not recommend creating an module (except nested ones) inside macro for example:

# instead of
import Example
@values [:values, :list]
defenum(MyApp.MyEnum, :type, @values)

# you could do
defmodule MyApp.MyEnum do
  import Example
  @values [:values, :list]
  defenum(:type, @values)
  # you can now access here all attributes you specify
end

Here is how usually I’m working with such macros:

defmodule Example do
  defmacro defenum(type, values) do
    quote bind_quoted: [type: type, values: values], unquote: false do
      @enum_type type
      @values Enum.map(values, &to_string/1)

      def type, do: @enum_type
      def values, do: @values
    end
  end
end

You can take a look at code of my last project:

Feel free in case of any questions.

Last Post!

halostatue

halostatue

It’s time available, because it’s a relatively large change to how my enums are currently created (it’s derived from ecto_enum but primarily switches to a string representation for the enums for a number of reasons). I like your overall approach better, but it’s going to take time that I don’t actually have to change how I manage the ~20 different enums used in our app.

Where Next?

Popular in Questions Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
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
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
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
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New

We're in Beta

About us Mission Statement