In Erlang, one can write '1' to get an integer-named atom. Elixir uses the syntax :<name> to define an atom, but :1 is not possible:
iex(1)> :1
** (SyntaxError) iex:1: unexpected token: ":" (column 1, codepoint U+003A)
Is there a way in Elixir to generate an integer-named atom?
You can put the 1 in quotes and prepend : to get the equivalent of '1' in Erlang:
iex(1)> :"1"
:"1"
iex(2)> :'1'
:"1"
You can also leverage string interpolation:
String.to_atom("#{your_number}")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With