使用整数键创建地图 [英] Create a Map with an Integer Key

查看:75
本文介绍了使用整数键创建地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)

推荐答案

要将Integer用作键,只需像这样使用它即可:

To use an Integer as a key, simply use it like this:

map = %{ 3 => "value" }


:3在Elixir中是无效值;在Elixir中,原子既不是字符串也不是整数,它们是常量,其名称就是它们的值.要使用仅以3作为原子的键,则必须使用以下命令:


: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" }

这篇关于使用整数键创建地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆