rmoorman
Does the `Kernel.pop_in/2` function actually do the right when facing entries being nil?
The documentation of Kernel.pop_in/2 states the following:
In case any entry returns
nil, its key will be removed and the deletion will be considered a success.
The accompanying example shows a nested map where the first entry (from the path passed to pop_in) is not found in the passed data:
iex> users = %{"john" => %{age: 27}, "meg" => %{age: 23}}
iex> pop_in(users, ["jane", :age])
{nil, %{"john" => %{age: 27}, "meg" => %{age: 23}}}
Nothing notable happens to the map as there is no "jane" key at all, as expected.
But how about "jane" being there but not having an age field or even not having any data assigned at all (yet)?
iex> users = %{"john" => %{age: 27}, "meg" => %{age: 23}, "jane" => %{}}
iex> pop_in(users, ["jane", :age])
{nil, %{"jane" => %{}, "john" => %{age: 27}, "meg" => %{age: 23}}}
Here an empty map was assigned to jane and we got an empty map for jane back. Still seems to be allright. But when the value for "jane" is set to nil, the pop_in/2 function will remove the "jane" key.
iex> users = %{"john" => %{age: 27}, "meg" => %{age: 23}, "jane" => nil}
iex> pop_in(users, ["jane", :age])
{nil, %{"john" => %{age: 27}, "meg" => %{age: 23}}}
This matches the description inside the documentation to the letter… Maybe it’s just me, but does it make sense to remove "jane" altogether while pop_in/2 targets the :age field within the data for "jane"?
In other words: Does it make sense to remove the player "Elmo" in the following example entirely when targeting the :cash field inside his :inventory but he actually hasn’t any associated data yet?
iex> players = %{"Elmo" => nil}
iex> pop_in(players, ["Elmo", :inventory, :cash])
{nil, %{}}
Most Liked
kip
I think the way to think about this is that Elmo does it fact have data - its just that the data is the atom nil.
ityonemo
It definitely feels wrong to me, though I also feel like you should never wind up in that situation. at the very least this behaviour should be documented.
kip
I think the documentation is very clear about this specific case. But it does seem unexpected.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








