#m.a vs m[:a]

5 messages · Page 1 of 1 (latest)

dull socket
#

What is the difference between these two ways of accessing elements of a map? I thought they were equivalent, but:

iex(11)> quote do: m[:a]
{{:., [], [Access, :get]}, [], [{:m, [], Elixir}, :a]}
iex(12)> quote do: m.a
{{:., [], [{:m, [], Elixir}, :a]}, [no_parens: true], []}
covert marsh
#

m.a will crash if there is no key :a

#

m[:a] will return nil instead

dull socket
#

Thankyou! So is Map.get(m, :a) equivalent to m[:a]?

covert marsh
#

it'll work the same for maps but it's not the same