Getting attributes from elixir into the BEAM module

How can I get attributes defined in my elixir module into the resultant BEAM file so I can see them from the outside. So for example if I have

defmodule RV do
  @author "robert"
  def f() do 42 end
end

how can I get the ‘author’ attribute into the resultant module? If I do RV.module_info() it is not there in the attributes field.

If Erlang Solutions: Elixir Module Attributes - Alchemy 101: Part 1 is anything to go by then attributes seem to be an entirely compile time construct.

Module attributes are evaluated at compile time, not runtime. Not knowing this can lead to confusion. All occurrences of the module attribute are replaced with whatever it evaluates to at compile time.

Just don’t take my word for it .

From the @ docs

Unlike Erlang, such attributes are not stored in the module by default since it is common in Elixir to use custom attributes to store temporary data that will be available at compile-time. Custom attributes may be configured to behave closer to Erlang by using Module.register_attribute/3.

So, it’s only a compile time construct in Elixir. Unless, I think, you use Module.register_attribute/3 with the :persist option?
I don’t know what Erlang Abstract Format. Since you’re asking this question, I’m going to assume it’s what you’re looking for on a complete whim.

2 Likes

Ok, I got curious and tested it.

iex> defmodule Test do                      
...>   Module.register_attribute(__MODULE__, :llama, persist: true)
...>   Module.register_attribute(__MODULE__, :lamb, [])
...>   @llama :Iwin
...>   @lamb :Ilose
...> end
{:module, Test,
 <<70, 79, 82, 49, 0, 0, 4, 12, 66, 69, 65, 77, 69, 120, 68, 99, 0, 0, 0, 94,
   131, 104, 2, 100, 0, 14, 101, 108, 105, 120, 105, 114, 95, 100, 111, 99, 115,
   95, 118, 49, 108, 0, 0, 0, 4, 104, 2, ...>>, :ok}
iex> Test.module_info                                            
[module: Test, exports: [__info__: 1, module_info: 0, module_info: 1],
 attributes: [vsn: [115500454799647980147165208311102157243], llama: [:Iwin]],
 compile: [options: [:debug_info], version: '7.1',
  source: 'c:/Users/Justin/Projects/sandbox/iex'],
 md5: <<86, 228, 148, 21, 58, 30, 26, 0, 77, 197, 110, 134, 67, 27, 177, 187>>]
3 Likes