Problem matchin map with ^key

I’m studying some tutorial about elixir

but I have a problem finding the solution in this example

nola = %{ name: "New Orleans", founded: 1718 }
field = "founded"
%{^field: city_founded} = nola
** (SyntaxError) iex:14: syntax error before: field


#I try with atom
field = :founded
%{^field: city_founded} = nola
** (SyntaxError) iex:15: syntax error before: field

#with Charlist
field = 'founded'
%{^field: city_founded} = nola 
** (SyntaxError) iex:16: syntax error before: field

In the manual it show that this is correct, but it didn’t work in my IEx 1.6.5 (compiled with OTP 19)

You can not pin an atom key. You need to use => syntax:

iex(1)> nola = %{name: "Foo"}
%{name: "Foo"}
iex(2)> field = :name
:name
iex(3)> %{^field => name} = nola
%{name: "Foo"}
iex(4)> name
"Foo"
2 Likes