Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't you have a variable as a key in a map literal?

Tags:

elixir

For example,

x = "foo"
%{foo => 1}

**(CompileError) iex:11: illegal use of variable x in map key

Obviously it's building the map at compile time, which could be more efficient, but why doesn't it fall back to a runtime constructor like most languages with data structure literals? Interested to know if there's something about the characteristics of maps in Elixir that I'm missing.

Adding to the confusion is this Google Groups message from over a year ago, where José Valim says:

Variable keys in maps will be available in the next Erlang release.

But that was a year ago, now, and I can't find any other references to this. To what Erlang version was he referring? I'm running 18 right now and it definitely still isn't in place.

like image 652
levand Avatar asked Oct 04 '15 12:10

levand


People also ask

What happens if key not found in map?

If the key is not present in the map, get() returns null. The get() method returns the value almost instantly, even if the map contains 100 million key/value pairs.

Are maps mutable in Go?

Maps are a mutable data structure, so you can modify them.


1 Answers

Erlang 18 added support for it and we need to update Elixir to leverage that. Therefore, we plan to support it on the next Elixir version, Elixir v1.2.

like image 94
José Valim Avatar answered Jan 02 '23 19:01

José Valim