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

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&query=perfume&page=2, I would like to get: ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New

Other popular topics Top

TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41539 114
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
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
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52341 488
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 126479 1222
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
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

Latest on Elixir Forum

We're in Beta

About us Mission Statement