Something that noone has mentioned yet, is that module attributes are only available in the module they are defined in. So if I have
defmodule A do
@pi 3.14159
end
And I have
defmodule B do
def pi, do: 3.14159
end
Then I can do this:
B.pi() # 3.14159
But I cannot get the constant value I’ve defined in A. Sometimes I’ll actually do something like
defmodule Example do
@constant "..."
def constant, do: @constant
end
The reason for this is inside the module it’s defined in I’d use it as a module attribute to quickly tell myself that this is a constant value. And the public function tells me that this constant is used elsewhere in the code, not just this module.


















