Map
我遇到了一个奇怪的问题。这是一个重现行为的小例子:
defmodule Document do
@expiration_presets_to_timeshift %{
"immediately": [seconds: 1],
"1-minute": [minutes: 1],
"1-hour": [hours: 1],
"1-day": [days: 1],
"1-week": [weeks: 1],
"1-month": [months: 1],
"1-year": [years: 1],
"never": [years: 9999],
}
def expiration_presets, do: @expiration_presets_to_timeshift
end
如果我们尝试从地图中检索信息:
iex(1)> import Document
Document
iex(2)> Document.expiration_presets
%{"1-day": [days: 1], "1-hour": [hours: 1], "1-minute": [minutes: 1],
"1-month": [months: 1], "1-week": [weeks: 1], "1-year": [years: 1],
immediately: [seconds: 1], never: [years: 9999]}
iex(3)> Document.expiration_presets["1-hour"]
nil
iex(4)> Map.get(Document.expiration_presets, "1-hour")
nil
我注意到两种奇怪的行为:
Map.get/2
和Map.[key]
都返回nil
)。为什么这样,我如何从地图中检索出来的值?答案 0 :(得分:2)
字符串键的映射语法如下所示:
%{"immediately" => [seconds: 1]}
更新地图以首先使用该样式。
此功能缺少:
def expiration_presets, do: @expiration_presets_to_timeshift