fireproofsocks

fireproofsocks

Module attributes: when must they be unique?

While looking over the source code for a library I’m using, I noticed that there were several times that the same module attribute (@params) was defined in the same module – the author was using this as a way to define default values for each function.

In a nutshell:

@params [:a, :b:, :c]
def a()
    # do something with @params
end

@params [:d, :e]
def b()
    # do something with @params
end

My IDE flagged these as invalid, but are they? My understanding was that these are evaluated at compile time.

Although there’s no mention of the uniqueness of module attributes on https://elixir-lang.org/getting-started/module-attributes.html I know that in test modules, it’s common to have multiple instances of the @tag attributes that decorates each function. Same for the @impl tag used when implementing behaviours.

Can someone help clarify what’s going on there and if that is valid?

Most Liked

LostKobrakai

LostKobrakai

Search for @my_data and you’ll find the example about that.

LostKobrakai

LostKobrakai

By default you can overwrite the value of a module attribute at any time, which is totally valid. If instantiated correctly they can also be used to aggregate data, which is how most of plugs functionality is working.

1player

1player

It’s definitely valid, but what’s going on?.. it depends.

@param :foo
@param :bar     
# here @param == :bar

But an attribute can also be registered to accumulate the values:

defmodule Foo do
  Module.register_attribute __MODULE__, :param, accumulate: true

  @param :foo
  @param :bar     
  # here @param == [:foo, :bar]
end

If you’re useing a module/macro system, sometimes it’s hard to tell how exactly these attributes are used. @tag in ExUnit probably isn’t accumulating its values, but using them everytime you call the test macro:

defmodule SomeTest do
  use ExUnit.Case

  @tag :foo  
  test "first test" do
    # here the `test` macro fetches the current value of @tag, which at this moment is :foo
  end

  @tag :bar 
  test "first test" do
    # here @tag == :bar
  end
end

I’m not too sure ExUnit works like this, but the general gist of this, and hopefully to answer your question, is that attributes can be set multiple times, and how are they used exactly depends on who’s consuming them :slight_smile:

Where Next?

Popular in Questions Top

New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
albydarned
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
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

Other popular topics 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
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
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
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

We're in Beta

About us Mission Statement