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

Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New

Other popular topics Top

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31142 143
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
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
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 126479 1222
New

We're in Beta

About us Mission Statement