Atoms: use cases and similarities with other langs

Do atoms work the same as const in JavaScript?

What are the major use cases for this type? I’m not sure I understand it’s importance.

Atoms in elixir are very similar to atoms in Erlang :smiley:

Well, like symbols in ruby are a better compare.

A const in JavaScript is just a variable that you can not change. But remember, objects behind references still can be changed.

Atoms though are values where the name is its own value. In code and errors they are nicely readable, while comparison is much quicker as it were if you were using strings instead, as they are represented as integers at runtime.

3 Likes

Julia has atoms too, and like elixir, they are used as critical tokens in AST parsing.

Atoms are more like symbols in JS or symbols in Ruby (especially pre-2.0).

1 Like

Js symbols are imho also a bad comparison. You‘ll get a unique symbol per reference you‘re creating and not per label you‘re using. Atoms are mapped to a unique integer per label though.

2 Likes

I wish I could find where I read this for reference, so I’ll paraphrase here, but it helped me to better understand atoms in Erlang/Elixir.

In a world of distributed nodes, an atom has the same meaning to any node in the cluster because its name is its value. Like 1 == 1 or a helium atom is helium atom anywhere in the universe, :ok == :ok or :error == :error will always have the same meaning to any node in the cluster and also has the benefit of conveying meaning to the person who is looking at it.

I’m going to go out on a limb here, but I’m willing to bet that’s why it’s called an atom :slight_smile:

4 Likes

@sasajuric used a great example today in a talk. Atoms are what in other languages might look like this.

const LABEL = 1
...
// use LABEL instead of 1 everywhere
3 Likes

Holy sh**. Mind blown.