Map.new/2 is quite useful and I use it frequently. Whenever I use it, I
expect that my transform function returns unique keys, so that
Enum.count(input)
== map_size(output) holds true. Unfortunately, this hasn’t always held true
for the data I’m working with, and it results in an output map that’s
essentially “lost” some of the input data. Here’s an example:
iex(2)> input = [%{name: "Bob", age: 37}, %{name: "Jill", age: 24}, %{name:
"Bob", age: 0}]
[%{age: 37, name: "Bob"}, %{age: 24, name: "Jill"}, %{age: 0, name: "Bob"}]
iex(3)> Map.new(input, &{&1.name, &1.age})
%{"Bob" => 0, "Jill" => 24}
Here the %{name: "Bob", age: 37} data has been lost because it was
overwritten by %{name: "Bob", age: 0} since maps obviously can’t have
duplicates. In this situation, it would be really nice if Map.new/2 did not
silently ignore the fact that I gave it duplicate keys. I’d like a strict
from of Map.new/2 that raises an error when your transform function returns
duplicate keys, to notify you that you aren’t using it properly.
If we add this, I’m not sure if it can just be a change to Map.new/2 (would
that be a breaking change for some users?) or if we’d need to add an
alternate function like Map.new!/2 or Map.strict_new/2 or something.
Thoughts?
Myron
--
You received this message because you are subscribed to the Google Groups
"elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/elixir-lang-core/5e15315d-b6c3-4fa7-b31f-a2d7c5576941%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.