Atoms
What atoms are
An atom is a constant whose name is its value. It carries no data of its own, it is written in the source and compared by identity, and all but the smallest programs use it for states, results and keys. The two most used atoms are true and false: they are plain atoms, and true and false are what the truthiness lesson builds its rule on.
erl
1> ok. ok 2> error. error 3> is_atom(ok). true 4> is_atom("ok"). false
ok and error are the two results most OTP functions answer with. "ok" is a string, a list of two character codes, and is_atom/1 tells them apart without looking at the text.
Writing an atom
An unquoted atom starts with a lowercase letter and continues with letters, digits, underscores and the at sign. That covers ok, not_found, node@host and the names of modules and functions. Anything with a capital, a space or punctuation needs single quotes, and the quotes are notation, not part of the value.
erl
1> max_value. max_value 2> 'Tuesday morning'. 'Tuesday morning' 3> 'Tuesday morning' =:= 'Tuesday morning'. true 4> hello@world. hello@world
Hello without quotes would be a variable, not an atom, and "hello" would be a string. The quoted form is also how an atom keeps a space or a dash in its name, which is why some libraries use atoms such as 'Content-Type' for a header name.
The atom table
Every atom in a running node lives in one global table. An atom is not copied between processes, it is not garbage collected, and the table has a fixed limit, one million and forty-eight thousand five hundred and seventy-six atoms by default. A program that builds atoms from user input, as a parser of arbitrary keys might, can fill the table and stop the node; the safe form is a map or a binary as the key.
erl
1> erlang:system_info(atom_limit). 1048576 2> erlang:system_info(atom_count) > 0. true
The count is how many the node has made so far; the text form is list_to_atom("..."), which is exactly the call to keep out of a loop over input. Atoms that appear in the source are made when the module is loaded, once, and cost nothing afterwards.
Atoms as results
A function that classifies its input returns an atom rather than a string: the caller matches the atom, and a typo becomes a compile-time or a match error instead of a silently different string. This program maps a status code to a label:
status.erl
-module(status). -export([main/0, label/1]). label(200) -> ok; label(404) -> not_found; label(_) -> unknown. main() -> io:format("~p~n", [label(404)]), io:format("~p~n", [label(200)]), io:format("~p~n", [label(500)]).
erlc status.erl
erl -noshell -eval 'status:main(), halt().'
not_found ok unknown
~p prints an atom without quotes because the atom needs none; the shell adds quotes only when it must. A caller can then write case status:label(Code) of ok -> ...; a clause that names another atom simply does not match, and the wrong name is visible in the source.
Converting between atoms and text
atom_to_list/1 returns the characters of the name as a string, and list_to_atom/1 goes back. Neither changes the atom table unless the atom is new. This program converts both ways and keeps a quoted atom with a space in it:
atoms.erl
-module(atoms). -export([main/0]). main() -> Atom = list_to_atom("ready"), io:format("~p~n", [Atom]), io:format("~s~n", [atom_to_list(Atom)]), io:format("~p~n", [is_atom(Atom)]), io:format("~s~n", [atom_to_list('Tuesday morning')]).
erlc atoms.erl
erl -noshell -eval 'atoms:main(), halt().'
ready ready true Tuesday morning
~p and ~s differ: the first prints a term the way the shell does, the second prints a string without quotes. A conversion back with list_to_atom/1 makes the atom if it does not exist yet, which is the call to keep away from input. The next lesson, Strings and binaries, takes the string side apart.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What is an atom?
Answer
A constant whose name is its value
An atom is a named constant:
okis the valueok, with no data inside it. -
Which of these is an atom written without quotes?
Answer
helloAn unquoted atom starts with a lowercase letter; an uppercase name is a variable.
-
When does an atom need single quotes?
Answer
When it starts with a capital or holds spaces or punctuation
The plain syntax allows lowercase letters, digits,
_and@; anything else needs quotes. -
What does
'Tuesday morning'.evaluate to?Answer
The atom
Tuesday morningSingle quotes build an atom, so the space is allowed and the quotes are part of the notation, not the value.
-
What does
is_atom(true).return?Answer
truetrueandfalseare ordinary atoms, andis_atom/1answerstruefor both. -
What does this module print?
-module(q3). -export([main/0]). main() -> io:format("~p~n", [is_atom('Hello')]).Answer
trueSingle quotes make
'Hello'an atom, sois_atom/1answerstrue. -
What does
list_to_atom("ready").return?Answer
readyThe function turns the characters into an atom, which the shell prints without quotes.
-
Why is building atoms from user input in a loop dangerous?
Answer
The atom table is global and bounded
Every new atom stays in the global table, so unbounded input can exhaust it and stop the node.
-
What does this module print?
-module(q1). -export([main/0, label/1]). label(200) -> ok; label(_) -> unknown. main() -> io:format("~p~n", [label(404)]).Answer
unknownThe second clause matches any value, including 404, so the label is
unknown. -
What does this module print?
-module(q2). -export([main/0]). main() -> A = list_to_atom("ready"), io:format("~p~n", [A =:= ready]).Answer
truelist_to_atom("ready")makes exactly the atomready, and strict equality compares it with itself.
The examples on this page were compiled and run with Erlang/OTP 27 (erts-15.2.7.14) on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Автор статьи: Arthur Isaev