Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is useful to have a atom type (like in elixir, erlang)?

According to http://elixir-lang.org/getting-started/basic-types.html#atoms:

Atoms are constants where their name is their own value. Some other languages call these symbols

I wonder what is the point of have a atom type. Probably to help build a parser or for macros? But in everyday use how it help the programmer?

BTW: Never use elixir or erlang, just note it exist (also in kdb)

like image 794
mamcx Avatar asked Aug 28 '15 00:08

mamcx


People also ask

Why use atoms Elixir?

Atoms are great for sending messages and representing constants; they're meant to be used as a developer tool. One of the most common use cases of atoms is to use them as the key of a key-value tuple pair, e.g., the response of an HTTP request. Atoms are everywhere in Elixir.

What is an atom in Erlang?

An atom is a literal, a constant with name. An atom is to be enclosed in single quotes (') if it does not begin with a lower-case letter or if it contains other characters than alphanumeric characters, underscore (_), or @.


3 Answers

They're basically strings that can easily be tested for equality.

Consider a string. Conceptually, we generally want to think of strings as being equal if they have the same contents. For example, "dog" == "dog" but "dog" != "cat". However, to check the equality of strings, we have to check to see if each letter in one string is equal to the letter in the same position in another string, which means that we have to walk through each element of the string and check each character for equality. This becomes a bit more cumbersome if dealing with Unicode strings and having to consider different ways of composing identical characters (for example, the character é has two representations in UTF-8).

It would be much simpler if we stored identical strings at the same location in memory. Then, checking equality would be a simple pointer or index comparison.

As a consequence of storing identical strings in the same location in memory, we can also store one copy of each unique kind of string regardless of how many times it is used in the program, thus saving some memory for commonly-used strings as well.

At a higher level, using atoms also lets us think of strings the same way we think of other primitive data types like integers.

like image 150
mipadi Avatar answered Oct 12 '22 16:10

mipadi


I think that one of the most common usage in erlang is to tag variables and messages, with the benefit of fast comparison (pattern match) as mipadi says.

For example you write a function that may fail depending on parameters provided, the status of connection to a server, or any reason. A very frequent usage is to return a tuple {ok,Value} in case of success, {error,Reason} in case of error. The calling function will have the choice to manage only the success case coding {ok,Value} = yourModule:yourFunction(Param...). Doing this it is clear that you consider only the success case, you extract directly the Value from the function return, it is fast, and you don't have to share any header with yourModule to decode the ok atom.

In messages you will often see things like {add,Key,Value}, {delete,Key},{delete_all}, {replace,Key,Value}, {append,Key,Value}... These are explicit messages, with the same advantages as mentioned before: fast,sensible,no share of header...

like image 32
Pascal Avatar answered Oct 12 '22 15:10

Pascal


Atoms are constants with itself as value. This is a concept very usefull in distributed systems, where constants can be defined differently on each system, while atoms are self-containing with no need for definement.

like image 2
h4cc Avatar answered Oct 12 '22 15:10

h4cc