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

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
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
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: <h1>Create Post</h1> <%= ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
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
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
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
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
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

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29377 241
New
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
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
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
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
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 47930 226
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New

We're in Beta

About us Mission Statement