Reputation: 12879
Why is the "params" in the Phoenix framework a map and not a hash? Can anyone also explain the inner implementation level details.
Upvotes: 3
Views: 1352
Reputation: 1466
Elixir introduced hashes/dicts as part of the core language, the Erlang VM does not have support for hashes. Underneath hashes are implemented on top of maps and keywords lists (list of paired tuples).
HashDict is implemented on top structs and structs are implemented on top maps.
it is confusing, future versions of elixir hashes/dicts are going to be deprecated there only going 2 data structures maps and mapsets.
Upvotes: 1
Reputation: 84170
From the getting started guide:
Note: Maps were recently introduced into the Erlang VM with EEP 43. Erlang 17 provides a partial implementation of the EEP, where only “small maps” are supported. This means maps have good performance characteristics only when storing at maximum a couple of dozens keys. To fill in this gap, Elixir also provides the HashDict module which uses a hashing algorithm to provide a dictionary that supports hundreds of thousands keys with good performance.
One of the key benefits of maps is partial pattern matching:
def edit(conn, %{"id" => id} = params)
...
The above will match for any map which contains the string id
as a key.
In OTP 18, maps have improved performance as you can see at https://gist.github.com/BinaryMuse/bb9f2cbf692e6cfa4841. And it is likely that HashDict
will be deprecated in the future.
There is some great information about Elixir data types in this answer: What is the benefit of Keyword Lists?
Upvotes: 6