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 Responses

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

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
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
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
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
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
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
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
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
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

We're in Beta

About us Mission Statement