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?
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.
1 Like
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 use
ing 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
1 Like
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?
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
I definitely agree on the confusion though!
1 Like
Search for @my_data
and you’ll find the example about that.
3 Likes