An atom is an object with a name that is identified only by the name itself.
Atoms are defined in Erlang using atom literals which are either
@
character, or1> hello.
hello
2> hello_world.
hello_world
3> world_Hello@.
world_Hello@
4> '1234'.
'1234'
5> '!@#$%% ä'.
'!@#$%% ä'
There are some atoms that appear in almost every Erlang program, in particular because of their use in the Standard Library.
true
and false
are the used to denote the respective Boolean valuesok
is used usually as a return value of a function that is called only for its effect, or as part of a return value, in both cases to signify a successful executionerror
is used to signify an error condition that doesn't warrant an early return from the upper functionsundefined
is usually used as a placeholder for an unspecified valueok
and error
are quite often used as part of a tuple, in which the first element of the tuple signals success while further elements contain the actual return value or error condition:
func(Input) ->
case Input of
magic_value ->
{ok, got_it};
_ ->
{error, wrong_one}
end.
{ok, _} = func(SomeValue).
One thing to keep in mind when using atoms is that they are stored in their own global table in memory and this table is not garbage collected, so dynamically creating atoms, in particular when a user can influence the atom name is heavily discouraged.