yayu
yayu

Reputation: 8088

Simple explanation of Erlang atom

I am learning Erlang and stuck trying to understand the concept of atoms. I know Python: What is a good explanation of these "atoms" in simple terms, or analogously with Python. So far, my understanding is that the type is like a string but without string operations?

Upvotes: 3

Views: 4584

Answers (3)

tkowal
tkowal

Reputation: 9289

Docs say that:

An atom is a literal, a constant with name.

Sometimes you have couple of options, that you would like to choose from. In C for example, you have enum:

enum Weekday { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday };

In C, it is really an integer, but you can use it in code as one of options. Atoms in Erlang are very useful in pattern matching. Lets consider very simple server:

loop() ->
    receive
        {request_type_1, Request} ->
            handle_request_1(Request),
            loop();
        {request_type_2, Request} ->
            handle_request_2(Request),
            loop();
        {stop, Reason} ->
            {ok, Reason};
        _ ->
            {error, bad_request}
    end.

Your server receives messages, that are two element tuples and uses atoms to differentiate between different types of requests: request_type_1, request_type_2 and stop. It is called pattern matching.

Server also uses atoms as return values. ok atom means, that everything went ok. _ matches everything, so in case, that simple server receives something unexpected, it quits with tuple {error, Reason}, where the reason is also atom bad_request.

Boolean values true and false are also atoms. You can build logical functions using function clauses like this:

and(true, true) ->
    true;
and(_, _) ->
    false.

or(false, false) ->
    false;
or(_, _) ->
    true.

(It is a little bit oversimplified, because you can call it like this: or(atom1, atom2) and it will return true, but it is only for illustration.)

Module names in Erlang are also atoms, so you can bind module name to variable and call it, for example type this in Erlang shell:

io:format("asdf").
Variable = io.
Variable:format("asdf").

You should not use atoms as strings, because they are not garbage collected. If you start creating them dynamically, you can run out of memory. They should be only used, when there is fixed amount of options, that you type in code by hand. Of course, you can use the same atom as many times as you want, because it always points to the same point in memory (an atom table).

They are better than C enums, because the value is known at runtime. So while debugging C code, you would see 1 instead of Tuesday in debugger. Using atoms doesn't have that disadvantage, you will see tuesday in your both in your code and Erlang shell.

Upvotes: 7

Derek Brown
Derek Brown

Reputation: 511

Also, they're often used to tag a tuple, for descriptiveness. For example:

{age, 42}

Rather than just

42

Upvotes: 3

zsoci
zsoci

Reputation: 75

Atom is a literal constant. Has no value but can be used as a value. Examples are: true, false, undefined. If you want to use it as a string, you need to apply atom_to_list(atom) to get a string (list) to work with. Module names are also atoms. Take a look at http://www.erlang.org/doc/reference_manual/data_types.html

Upvotes: 2

Related Questions