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

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New

Other Trending Topics Top

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
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews