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.

Where Next?

Popular in Questions Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New
stefanchrobot
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
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New
lanycrost
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

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 42920 311
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30877 112
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43622 214
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New

We're in Beta

About us Mission Statement