Confused about "Elixir." prefix in interpolation results

I have this list:

iex(112)> myLetters
[A, B, C, D, E, F, G, H]

I can grab the first element like this:

iex(113)> elem(List.pop_at(myLetters,0),0)
A

But when is use interpolation, I see this:

iex(114)> "#{elem(List.pop_at(myLetters,0),0)}"
"Elixir.A"

My questions:

  1. Why the “Elixir” prefix?
  2. How do I prevent/filter out that prefix?

Minimal reproducable version:

iex(1)> "#{A}"
"Elixir.A"
  1. A is an atom. In Elixir, upper-case atoms are assumed to be module names, which are prefixed with Elixir. by the compiler.
  2. You probably want to use strings, not atoms. Or define the atoms explicitly using :"A" syntax:
iex(2)> "#{:"A"}"
"A"
4 Likes

Ah! Classic newbie mistake. That makes perfect sense.

Thank you so much!

1 Like

To be more accurate, I should have said that A is assumed to be a module name, which is converted to the :"Elixir.A" atom by the compiler.

1 Like

You would also find that inspect/2 will correctly introspect a module:

iex> my_letters = [A, B, C, D, E, F, G, H]
[A, B, C, D, E, F, G, H]
iex> "#{elem(List.pop_at(my_letters,0),0)}"
"Elixir.A"
iex> "#{elem(List.pop_at(my_letters,0),0) |> inspect()}"
"A"

Also, the idiomatic form of identifiers in Elixir is “snake_case” rather than “camelCase”.

2 Likes

Small additions:

  • :"A" could simply be written as :A (which is how the formatter will rewrite it).
  • This guide explains how modules are just atoms prefixed by Elixir.
1 Like