Module Attributes in Elixir

Published October 08, 2018 by Toran Billups

In part 14 of my Elixir journey I'll briefly show how module attributes work.

Attributes

You can add metadata to an Elixir module by using the `@` symbol. This attribute must be declared within the module but not inside a function.

    defmodule Foo do
      @name "Toran"
      def speak() do
        IO.puts "name was #{@name}"
      end
    end
  

You can use this attribute from within a function as shown above. You typically see attributes used for configuration, metadata or what some consider to be a constant.


Buy Me a Coffee

Twitter / Github / Email