Module attributes

In Elixir, a module attribute is a name with an @ in front of it and a value attached in the module body. The compiler reads every use of the name and puts the value in its place, so an attribute is a constant of the source rather than a variable of the program. Elixir uses the same syntax for two different jobs: a value a module needs, such as a limit or a default, and the documentation the tools read.

An attribute is a constant

The attribute is set once and read in the functions of the module. Because the value is substituted at compile time, it can appear in a guard and in a default argument, which a variable of the body cannot do:

limits.exs

defmodule Limits do @max 3 @name "demo" def max, do: @max def check(n) when n <= @max, do: :ok def check(_n), do: :too_big def name, do: @name end IO.puts(Limits.max()) IO.puts(Limits.check(2)) IO.puts(Limits.check(5)) IO.puts(Limits.name())

elixir limits.exs

3
ok
too_big
demo

Changing @max changes the guard, the function and the documentation together, because every use is the same compile-time constant. An attribute is not module state: no function can read it at run time with a name, and a value that changes while the program runs belongs in a process or in a function argument.

@moduledoc and @doc

Two attributes are reserved for documentation. @moduledoc describes the module and @doc describes the function below it; the h helper of iex and the generated documentation read them:

docs.exs

defmodule Greeter do @moduledoc "Greets people." @doc "Greets one person by name." def hello(name), do: "hello #{name}" end IO.puts(Greeter.hello("Ada"))

elixir docs.exs

hello Ada

@doc false hides a public function from the documentation when it is an implementation detail that still has to be public, such as a callback of a behaviour. A function without @doc is documented as having no documentation, which a project that publishes its docs usually treats as a gap.

Read at compile time

Every attribute is evaluated while the module is compiled, so its value cannot come from the run. A default argument that reads one is fixed in the compiled function:

default.exs

defmodule Client do @timeout 5_000 def call(url, timeout \\ @timeout), do: {url, timeout} end IO.inspect(Client.call("https://example.com")) IO.inspect(Client.call("https://example.com", 1_000))

elixir default.exs

{"https://example.com", 5000}
{"https://example.com", 1000}

A value that must be read from the environment at start belongs in runtime.exs or in a call at run time, which the configuration lesson shows. The attribute is the right tool for a constant of the code, not for a setting of the machine. The @version and @behaviour attributes a project already uses are the same mechanism.

The mistake: a doc with no function

@doc belongs directly above the function it describes. When the function that should follow is missing, the attribute has nothing to attach to, and the compiler warns that it was set with no definition after it:

nodoc.exs

defmodule Docs do @doc "The answer" end IO.puts("compiled")

elixir nodoc.exs

compiled
    warning: module attribute @doc was set but no definition follows it
    │
  2 │ @doc "The answer"
    │ ~~~~~~~~~~~~~~~~~
    │
    └─ nodoc.exs:2: Docs (module)

The module compiles and the warning names the attribute, the line it sits on and the module. The usual cause is a function that was removed while its documentation stayed, or a blank line or another attribute placed between the two: the compiler attaches @doc to the next definition, and a comment does not break the link but a function does. Read the warning as a stale line of documentation, not as a detail of the build.

The programs on this page were run with Elixir 1.19.7 on Erlang/OTP 27 (erts 15.2.7.14), on Linux x86_64 in a Docker container.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. When is the value of a module attribute read?

    Answer

    While the module is compiled, and the value is put in its place

    Attributes are compile-time constants, which is why a guard and a default can use one.

  2. Can a function change a module attribute while the program runs?

    Answer

    No, the attribute is not part of the run-time state

    An attribute is source-level; mutable state lives in a process or in an argument.

  3. What does @doc false do?

    Answer

    It hides a public function from the documentation

    The function stays public, and the documentation tools leave it out.

  4. What does the compiler warn about when @doc has no function below it?

    Answer

    module attribute @doc was set but no definition follows it

    The attribute is attached to the next definition, and without one it is a warning.

  5. What does this program print? defmodule Limits do @max 3 def max, do: @max end IO.puts(Limits.max())

    Answer

    3

    The attribute value is substituted into the function, so the call returns 3.

  6. What does this program print? defmodule Limits do @max 3 def check(n) when n <= @max, do: :ok def check(_n), do: :too_big end IO.puts(Limits.check(5))

    Answer

    too_big

    The attribute is a compile-time integer, so the guard compares 5 with 3.

  7. What does this program print? defmodule Client do @timeout 5_000 def call(url, timeout \\ @timeout), do: {url, timeout} end IO.inspect(Client.call("example.com"))

    Answer

    {"example.com", 5000}

    The default is the compiled value of the attribute, so the missing argument becomes 5000.

  8. What does this program print? defmodule Greeter do @name "demo" def name, do: "the #{@name} module" end IO.puts(Greeter.name())

    Answer

    the demo module

    The attribute is substituted inside the string before the function is compiled.

  9. Which attribute describes a whole module?

    Answer

    @moduledoc

    @moduledoc is the documentation of the module; @doc is the one of a function.

  10. Why is a module attribute a better fit than an argument for a fixed limit?

    Answer

    It is written once and used in guards, defaults and functions

    One constant of the source feeds every place that needs it, including places a variable cannot reach.

Автор статьи: Arthur Isaev