Module Attributes

Elixir modules each have associated metadata. Each item of metadata is called an attribute of the module and is identified by a name. Inside a module, you can access these attributes by prefixing the name with an at sign (@).

You can give an attribute a value using the syntax

 
@name​ value

This works only at the top level of a module—you cannot set an attribute value inside a function definition. You can, however, access attributes inside functions.

mm/attributes.exs
 
defmodule​ Example ​do
 
@author​ ​"Dave Thomas"
 
def​ get_author ​do
 
@author
 
end
 
end
 
 
IO.puts ​"Example was written by #{Example.get_author}"

You can set the same attribute multiple times in a module. If you access that attribute in a named ...

Get Programming Elixir now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.