fireproofsocks

fireproofsocks

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?

Showing Posts 1 to 5

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:

fireproofsocks

fireproofsocks OP

Mind. Blown. This makes me have more questions. So if you have something like this:

defmodule X do
   @y 1 
   @y 2 
   @y 3 

   def y, do: @y

end

What does X.y() return? 3?

And why isn’t this mentioned on the https://elixir-lang.org/getting-started/module-attributes.html page?

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!

LostKobrakai

LostKobrakai

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

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews