I want to create a map with a key of type integer, but this doesn't work:
iex(1)> a = %{3: "fdsfd"}
** (SyntaxError) iex:1: unexpected token: ":" (column 8, codepoint U+003A)
iex(1)> a = %{:3 => "fdsfd"}
** (SyntaxError) iex:1: unexpected token: ":" (column 7, codepoint U+003A)
To use an Integer as a key, simply use it like this:
map = %{ 3 => "value" }
:3
is an invalid value in Elixir; atoms are neither Strings or Integers in Elixir, they are constants where their name is their value. To use a key with only 3
as an atom, you would have to use this:
map = %{ :"3" => "value" }
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