Tracks
/
Elixir
Elixir
/
Syllabus
/
Module Attributes As Constants
Mo

Module Attributes As Constants in Elixir

5 exercises

About Module Attributes As Constants

Module attributes may be used like "constants" which are evaluated at compile-time.

defmodule Example do
  @number 2

  def number(), do: @number
end

However, they don't strictly behave like constants because they can be overwritten by redefining them in the module:

defmodule Example do
  @standard_message "Hello, World!"
  @standard_message "Overwritten!"

  def message(), do: @standard_message
end
Edit via GitHub The link opens in a new window or tab

Learn Module Attributes As Constants

Practicing is locked

Unlock 4 more exercises to practice Module Attributes As Constants