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 give an attribute a value using the syntax

 @name value

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

 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 function ...

Get Programming Elixir ≥ 1.6 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.