Capital letter Atoms in Elixir treated differently in Maps!

I learnt that any name starting with capital letter is treated as an Atom. (e.g. Module names are atoms, and are represented internally with prefix: Atom == :"Elixir.Atom")

So I tried using capital letter keys, just to see how it behaves elsewhere:

%{:name => "Bob", :age => 25, :works_at => "Initech"}

# Gets shortened to:
%{age: 25, name: "Bob", works_at: "Initech"}

But the capital letter atoms are not shortened like that:

%{Name => "Bob", Age => 25, WorksAt => Initech}

# Remains same:
%{Age => 25, Name => "Bob", WorksAt => Initech}

%{:"na me" => "Bob", :"Age" => 25, :"works at" => "Initech"}

# Changes to:
%{Age: 25, "na me": "Bob", "works at": "Initech"}

%{:name => "Bob", :"Elixir.Age" => 25, :"WorksAt" => "Initech"}

%{Age => 25, :WorksAt => "Initech", :"name" => "Bob"}

P.S. Don’t ask these kinds of questions in Interview, or use it in code base. :sweat_smile:

Age: is equivalent to :Age. As foo: is equivalent to :foo. :slight_smile:

5 Likes

More precisely, it’s treated as an alias.

2 Likes