Skip to content

Add :skip to Access.get_and_update and fix nil handling in pop_in - #16192

Open
josevalim wants to merge 2 commits into
mainfrom
jv-skip-get-and-update
Open

josevalim wants to merge 2 commits into
mainfrom
jv-skip-get-and-update

Conversation

@josevalim

Copy link
Copy Markdown
Member

Access.get_and_update callbacks may now return :skip, which returns the current value and leaves the container unchanged: missing keys are not inserted and existing keys are not removed. This is supported by Map.get_and_update/3, Map.get_and_update!/3,
Keyword.get_and_update/3, Keyword.get_and_update!/3, and all accessors in the Access module.

pop_in/1 and pop_in/2 now use :skip so they only ever pop the last entry in the path. Previously, a nil value in the middle of the path removed the key that held it, and the two functions handled nil differently: pop_in/2 raised on a nil root while pop_in/1 returned {nil, nil} for single-key paths and raised on deeper paths, and pop_in/1 also raised when nil appeared three or more levels deep. Now a nil or missing value anywhere before the last key returns the data unchanged, and a nil root returns {nil, nil}.

Custom Access implementations and accessor functions must handle :skip, as pop_in/1 and pop_in/2 return it when the path goes through a nil value.

Access.get_and_update callbacks may now return :skip, which returns
the current value and leaves the container unchanged: missing keys
are not inserted and existing keys are not removed. This is supported
by Map.get_and_update/3, Map.get_and_update!/3,
Keyword.get_and_update/3, Keyword.get_and_update!/3, and all accessors
in the Access module.

pop_in/1 and pop_in/2 now use :skip so they only ever pop the last
entry in the path. Previously, a nil value in the middle of the path
removed the key that held it, and the two functions handled nil
differently: pop_in/2 raised on a nil root while pop_in/1 returned
{nil, nil} for single-key paths and raised on deeper paths, and
pop_in/1 also raised when nil appeared three or more levels deep.
Now a nil or missing value anywhere before the last key returns the
data unchanged, and a nil root returns {nil, nil}.

Custom Access implementations and accessor functions must handle
:skip, as pop_in/1 and pop_in/2 return it when the path goes
through a nil value.
@josevalim josevalim added this to the v1.22 milestone Nov 23, 2026
@lukaszsamson

Copy link
Copy Markdown
Contributor

nil root returns {nil, nil}

The docs say pop_in(opts[:foo][:bar]) is equivalent to pop_in(opts, [:foo, :bar]) but currently with nil root one form returns {nil, nil}, and the other raises. Does this PR address that?

@josevalim

Copy link
Copy Markdown
Member Author
Path Before After
nil root, pop_in(nil, [:a]) raises "could not pop key :a on a nil value" {nil, nil}
nil root, pop_in(z[:a]) {nil, nil} {nil, nil}
nil root, pop_in(z[:a][:b]) raises "could not put/update key :a on a nil value" {nil, nil}
%{a: nil, keep: 1}, path [:a, :b] {nil, %{keep: 1}} (:a removed) {nil, %{a: nil, keep: 1}}
%{a: nil, keep: 1}, pop_in(m[:a][:b][:c]) raises on :b {nil, %{a: nil, keep: 1}}
%{a: nil, keep: 1}, pop_in(m, [:a, :b, :c]) {nil, %{keep: 1}} (:a removed) {nil, %{a: nil, keep: 1}}
%{}, pop_in(e[:a][:b][:c]) raises on :b {nil, %{}}
%{f: nil}, pop_in(s.f[:b][:c]) raises on :b {nil, s}
%{a: nil}, pop_in(m, [:a]) (last key) {nil, %{}} unchanged, still {nil, %{}}

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants