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

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
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
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
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
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44608 311
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
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement