rmoorman

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

kip

ex_cldr Core Team

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

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

kip

ex_cldr Core Team

I think the documentation is very clear about this specific case. But it does seem unexpected.

Where Next?

Popular in Questions Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New

Other popular topics Top

yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New

We're in Beta

About us Mission Statement