Erlang Language Atoms


Example

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

  • an unquoted string that starts with a lowercase letter and contains only letters, digits, underscores or the @ character, or
  • A single quoted string

Examples

1> hello.
hello

2> hello_world.
hello_world

3> world_Hello@.
world_Hello@

4> '1234'.     
'1234'

5> '!@#$%% ä'.
'!@#$%% ä'

Atoms that are used in most Erlang programs

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 values
  • ok 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 execution
  • In the same way error is used to signify an error condition that doesn't warrant an early return from the upper functions
  • undefined is usually used as a placeholder for an unspecified value

Use as tags

ok 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).

Storage

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.