It indeed looks nicer, but that would be a breaking change, and maybe more importantly, subduing two different cases to one behavior. Sure, in your case you don't need to differentiate between an empty map and nil-instantiated map, but somebody else does. Perhaps you could change the upstream code to send you only nil key stripped maps or only fully instantied maps (e. g. structs).
-------- Original Message -------- On 22 Jun 2022, 14:37, Nganga Mburu wrote: > We've used `map[key] || default_value`, or even `Map.get(map, key) || > default_value` before but I felt this being handled my `Map.get/3` would be > better. > > `Map.get(map, key, default_value)` looks better than `Map.get(map, key) || > default_value` > > On Wed, Jun 22, 2022 at 11:32 AM 'Andrey Yugai' via elixir-lang-core > <elixir-lang-core@googlegroups.com> wrote: > >> For this behavior I think you'd be better of using `map[key] || >> default_value`, or even `Map.get(map, key) || default_value`. Docs for >> `Map.get/3` are pretty clear about when it returns default value. >> >> -------- Original Message -------- >> On 22 Jun 2022, 14:25, Nganga Mburu < mburungan...@gmail.com> wrote: >> >>> I've found myself and seen others expecting to receive the default value >>> from `Map.get/3` when the value is `nil`. >>> >>> An example is: >>> `Map.get(%{name: nil}, :name, "John")` >>> >>> Expected it to return "John" but it returns `nil` >>> >>> My thinking is since the default value is for the key then when the key >>> value is `nil` we should be able to get the default value. >>> >>> The change in code could be as below: >>> ``` >>> def get(map, key, default \\ nil) do >>> case map do >>> %{^key => value} when is_nil(value) -> >>> default >>> >>> %{^key => value} -> >>> value >>> >>> %{} -> >>> default >>> >>> other -> >>> :erlang.error({:badmap, other}, [map, key, default]) >>> end >>> end >>> ``` >>> >>> -- >>> 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 elixir-lang-core+unsubscr...@googlegroups.com. >>> To view this discussion on the web visit >>> [https://groups.google.com/d/msgid/elixir-lang-core/9d31c830-8005-4a76-a1f9-dbea2d841ea4n%40googlegroups.com](https://groups.google.com/d/msgid/elixir-lang-core/9d31c830-8005-4a76-a1f9-dbea2d841ea4n%40googlegroups.com?utm_medium=email&utm_source=footer). >>> >>> -- >>> 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 elixir-lang-core+unsubscr...@googlegroups.com. >>> To view this discussion on the web visit >>> [https://groups.google.com/d/msgid/elixir-lang-core/Vn4lCPxz8hge1GGBTOw5gKy_9JfNcg4Zau0qqixm43Qjl73cEetpnKkhjYaadr9tUNRnBCBXiCeesQRjUSbXT9OXNuwHXXLL7k3rZ6STH3Y%3D%40pm.me](https://groups.google.com/d/msgid/elixir-lang-core/Vn4lCPxz8hge1GGBTOw5gKy_9JfNcg4Zau0qqixm43Qjl73cEetpnKkhjYaadr9tUNRnBCBXiCeesQRjUSbXT9OXNuwHXXLL7k3rZ6STH3Y%3D%40pm.me?utm_medium=email&utm_source=footer). > > -- > 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 elixir-lang-core+unsubscr...@googlegroups.com. > To view this discussion on the web visit > [https://groups.google.com/d/msgid/elixir-lang-core/CAFVCX%3D0KfetF09s9ofC4KTT-JCNv9eiKm42%2B4E--qFr29GJgkQ%40mail.gmail.com](https://groups.google.com/d/msgid/elixir-lang-core/CAFVCX%3D0KfetF09s9ofC4KTT-JCNv9eiKm42%2B4E--qFr29GJgkQ%40mail.gmail.com?utm_medium=email&utm_source=footer). -- 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 elixir-lang-core+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/3u1sVNHNWbB8ACU1BJFQ9g5JIqsWD0XQ4nSrRJOUWL0W0gv7wXyw7-djJWi62oM56MN4J38B86gityF6pzir1OUppfwFPtPubQ4PG_ixA10%3D%40pm.me.