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:

1player

1player

Yes, X.y() == 3.

I don’t think it’s mentioned because they work like you’d expect variables to work. If you saw:

y = 3
y = 4

It would be pretty obvious that y is 4 :slight_smile:

I definitely agree on the confusion though!

Where Next?

Popular in Questions Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
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
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
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
qwerescape
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
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
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...
977 41022 311
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
gshaw
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
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
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
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
belgoros
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
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

We're in Beta

About us Mission Statement