ImNotAVirus

ImNotAVirus

`Module.delete_attribute/2` doesn't reset `accumulate: true`

Today I encountered an weird behavior when I tried to change the accumulate option for a module attribute.

I would like to know if this behavior is intended since the documentation does not mention it.

In summary the steps are:

  • Create a module attribute with the accumulate: true option
  • Delete the module attribute
  • Recreate it with the accumulate: false option
  • Try to assign values, they will still be accumulated

Here is an example :

defmodule ModuleDeleteTest do
  # 1/ Define a module attribute with accumulate: true
  Module.register_attribute(__MODULE__, :attribut, accumulate: true)
  
  # 2/ Then delete and recreate the module attribute but with accumulate: false
  Module.delete_attribute(__MODULE__, :attribut)
  Module.register_attribute(__MODULE__, :attribut, accumulate: false)

  # Unfortunately, the accumulate: true flag is still present
  @attribut :foo
  @attribut :bar
  
  # > CURRENT attribut value: [:bar, :foo]
  IO.inspect(@attribut, label: "CURRENT attribut value")
end

On this example I was expecting@attribut to be equals to :bar at the end.

After examining the Elixir source code, I noticed that delete_attribute/2 with accumulate: true does remove the attribute from the bag but not from the set. As a result, the set still contains the accumulate: true flag.

Here here a quick fix:

defmodule ModuleFix do
  def delete_attribute(module, key) do
    {set, _bag} = :elixir_module.data_tables(module)
    
    # Here bag is clean by Module.delete_attribute/2
    Module.delete_attribute(module, key)
    
    # But you also need to clean the set or the module attribute
    # still have the :accumulate flag set
    :ets.delete(set, key)
  end
end

defmodule ModuleDeleteTestFix do
  # 1/ Define a module attribute with accumulate: true
  Module.register_attribute(__MODULE__, :attribut, accumulate: true)
  
  # 2/ Then delete and recreate the module attribute but with accumulate: false
  ModuleFix.delete_attribute(__MODULE__, :attribut)
  Module.register_attribute(__MODULE__, :attribut, accumulate: false)

  # Now, module attribute is properly reset
  @attribut :foo
  @attribut :bar
  
  # > FIXED field value: :bar
  IO.inspect(@attribut, label: "FIXED attribut value")
end

As said in the introduction, I don’t know if this behavior is intended and if it is, I think it should be mentioned in the doc If it’s not, I’d be happy to open an issue on Github and propose a PR.

Marked As Solved

Where Next?

Popular in Questions Top

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 record...
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
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
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
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
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
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
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
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
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

We're in Beta

About us Mission Statement