An Atom in Elixir (and Erlang) is a literal, a constant with a name. Elixir atom is similar to Ruby’s symbol in that the VM checks if it’s already defined in memory and reuses it, instead of re-evaluating it (like in the case of strings).
There are some special uses of atoms:
Modules
Elixir modules are represented using atoms.
iex> is_atom(String)
true
iex> Atom.to_string(String)
"Elixir.String"
iex> :"Elixir.String"
String
Nil
In Elixir nil
is of the type atom.
iex> is_atom(nil)
true
iex> :nil
nil
True and False
In Elixir (and Erlang), true
and false
are also atoms.
iex> is_atom(true)
true
iex> is_atom(false)
true
iex> :true
true
Since atoms are stored in memory, it makes sense to use it for nil
, modules
and boolean values. Similary in a project, atoms should be used for literals
that exist throughout the project, and not used for dynamically defined values
in a project.