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:
- Why the “Elixir” prefix?
- How do I prevent/filter out that prefix?
adamu
2
Minimal reproducable version:
iex(1)> "#{A}"
"Elixir.A"
-
A
is an atom. In Elixir, upper-case atoms are assumed to be module names, which are prefixed with Elixir.
by the compiler.
- 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
adamu
4
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
kip
5
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