使用整数键创建映射

时间:2017-04-05 02:08:06

标签: dictionary elixir

我想创建一个带有整数类型键的映射,但这不起作用:

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)

1 个答案:

答案 0 :(得分:4)

要将Integer用作键,只需使用它:

map = %{ 3 => "value" }

:3是Elixir中的无效值;原子既不是Elixir中的字符串也不是整数,它们是常量,其名称是它们的值。要使用仅3作为原子的键,您必须使用:

map = %{ :"3" => "value" }
相关问题