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.
Popular in Questions
What is the idiomatic way of matching for not nil in Elixir?
E.g.,
First way:
defp halt_if_not_signed_in(conn, signed_in_account) when...
New
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
Hello all!
I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
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
Hi,
I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
Hello, I have map which I want to convert it to string like this:
the map:
%{last_name: "tavakkoli", name: "shahryar"}
the string I ne...
New
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size?
Thanks
New
Other popular topics
Credo is smart enough to check for (something like) this:
assert length(the_list) == 0
with this response:
Checking if an enum is empt...
New
Hello all!
I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible.
total = 10
while total != 0
...
New
I have a User schema with a :from_id field set to type :string:
defmodule TweetBot.Repo.Migrations.CreateUsers do
use Ecto.Migration
...
New
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
Hi everyone,
One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors:
[WARN] - (starship::utils): Executing command ...
New
Hi everyone!
I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New







