I have this module in elixir with an attribute:
defmodule MyAwesomeModule do
@awesome_number 7
# other stuff...
end
I'm unable to access @awesome_number
outside the module. I've tried using the Module.get_attribute/2
method, but it throws this error:
iex(79)> Module.get_attribute(MyAwesomeModule, :awesome_number)
** (ArgumentError) could not call get_attribute on module MyAwesomeModule because it was already compiled
(elixir) lib/module.ex:1101: Module.assert_not_compiled!/2
(elixir) lib/module.ex:1016: Module.get_attribute/3
So right now, I'm wrapping the module attribute in a method to access it, but it doesn't really make sense to me. I could simply use the method and stop using the attribute all together:
defmodule MyAwesomeModule do
@awesome_number 7
def awesome_number, do: @awesome_number
# other stuff...
end
So my question is, is there a better / proper way of doing this?