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?
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixir-ls
- #ai
- #elixirconf-us
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 5- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
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
It’s definitely valid, but what’s going on?.. it depends.
But an attribute can also be registered to accumulate the values:
If you’re
useing a module/macro system, sometimes it’s hard to tell how exactly these attributes are used.@tagin ExUnit probably isn’t accumulating its values, but using them everytime you call thetestmacro: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
fireproofsocks
Mind. Blown. This makes me have more questions. So if you have something like this:
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
Yes,
X.y()== 3.I don’t think it’s mentioned because they work like you’d expect variables to work. If you saw:
It would be pretty obvious that
yis 4I definitely agree on the confusion though!
LostKobrakai
Search for
@my_dataand you’ll find the example about that.