I have a key as a variable, I want to create a Map with a key :key1 or “key2” using one of the variables, but I’m unable to:
iex(24)> a1 = :key1
:key1
iex(25)> a2 = "key2"
"key2"
iex(26)> %{a1: 123}
%{a1: 123}
iex(27)> %{:a1 => 123}
%{a1: 123}
iex(28)> %{^a2 => 123}
** (CompileError) iex:28: cannot use ^a2 outside of match clauses
(stdlib) lists.erl:1354: :lists.mapfoldl/3
iex(28)> %{^a1 => 123}
** (CompileError) iex:28: cannot use ^a1 outside of match clauses
(stdlib) lists.erl:1354: :lists.mapfoldl/3