venomnert

venomnert

Module Attribute behaviour during compilation

I am just doing some poking around with module attribute and I created the following code:

defmodule ModuleAttribute do
  Module.register_attribute(__MODULE__, :test_attribute, accumulate: true)
  @test_attribute 1
  @test_attribute 10
  def increase(), do: IO.inspect(@test_attribute, label: "Value")
  @test_attribute 20
end

When I run iex -S mix i get the following values

iex(40)> ModuleAttribute.increase     
Value: [10, 1]

If my understanding of elixir compilation is correct, should it be [1, 10, 20]? I am not sure why 20 didn’t get added in during compilation.

Most Liked

OvermindDL1

OvermindDL1

For note, module attributes are a lot like normal bindings, just saving you from typing the unquote part.

So this:

defmodule ModuleAttribute do
  @thing 1
  @thing 2
  def blah(), do: @thing # returns 2
  @thing 3
end

Is the same as this:

defmodule ModuleAttribute do
  thing = 1
  thing = 2
  def blah(), do: unquote(thing) # returns 2
  thing = 3
end

And thus this with an accumulated attribute:

defmodule ModuleAttribute do
  Module.register_attribute(__MODULE__, :thing, accumulate: true)
  @thing 1
  @thing 2
  def blah(), do: @thing # returns [2, 1]
  @thing 3
end

Is the same as this:

defmodule ModuleAttribute do
  thing = []
  thing = [1 | thing]
  thing = [2 | thing] # So this is now [2, 1]
  def blah(), do: unquote(thing) # returns [2, 1]
  thing = [3 | thing]
end
LostKobrakai

LostKobrakai

This is correct. Module attributes always have the value assigned to it in lines above the call. Basically you “hardcode” the parameter of IO.inspect at the time line 5 is compiled.

NobbZ

NobbZ

20 isn’t in the list, because you only add it to the list after reading it. Each usage of a module attribute will only ever know about values that have been assigned before its usage, pretty much like regular variables.

Where Next?

Popular in Questions Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
Fl4m3Ph03n1x
Background Let’s assume I have a typical GenServer that receives messages as requests, does some operation in a DB and returns responses....
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

We're in Beta

About us Mission Statement