rmoorman

rmoorman

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, %{}}

Showing Posts 1 to 6

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.

rmoorman

rmoorman OP

Do you mean that it does not feel right to you either because Elmo’s data (even though it is “just” nil) is removed entirely? (which is indeed what the op is about)

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.

ityonemo

ityonemo

Wow. I missed that sentence! Maybe it should be put in a warning block!

rmoorman

rmoorman OP

I also mentioned in the OP that pop_in/2 behaves exactly as it is documented. I also missed that initially so that could be emphasized :slightly_smiling_face:

Anyway, my point is to question whether it is sensible to remove the data altogether. I am glad that there are others finding the current behavior counter-intuitive to a varying degree!

It seems that the pop_in/2 function came to be as a renamed modification of a delete_in/2 function. Maybe it does make a little more sense in the context of deleting things nested inside a data structure, I don’t know.
Anyway, I hope he doesn’t mind dragging him into this, but @josevalim, what is your point of view in this matter?

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews