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
@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
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.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #elixirconf-us
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










