# Could pop!(::Dict, key, default) have a do-block method?

**URL:** <https://discourse.julialang.org/t/could-pop-dict-key-default-have-a-do-block-method/55378>\
**Category:** General Usage\
**Tags:** question, dictionary\
**Created:** [February 16, 2021, 9:26am UTC](https://discourse.julialang.org/t/could-pop-dict-key-default-have-a-do-block-method/55378 "2021-02-16T09:26:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [February 16, 2021, 9:26am UTC](https://discourse.julialang.org/t/could-pop-dict-key-default-have-a-do-block-method/55378/1 "2021-02-16T09:26:05Z")

</div>

Currently `pop!` has a method that requires all arguments to be evaluated beforehand:

```julia
julia> a = Dict("x" => 3, "y" => 4)
Dict{String,Int64} with 2 entries:
  "x" => 3
  "y" => 4

julia> @time pop!(a, "x", (sleep(10); 5))
 10.000316 seconds (1.00 k allocations: 22.312 KiB)
3

```

However in this case there’s no need to evaluate the default argument. Would it make sense to add a method analogous to `get!` that accepts a function as the first argument, so that it’s possible to pass something like this:

```julia
julia> pop!(a, "x") do 
         sleep(10)
         return 5
       end

```

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [February 16, 2021, 9:34am UTC](https://discourse.julialang.org/t/could-pop-dict-key-default-have-a-do-block-method/55378/2 "2021-02-16T09:34:59Z")

</div>

Feature requests are better on GitHub.  
Even if you are not sure they are s good idea yet, it is best to have them with the others.

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [February 16, 2021, 9:49am UTC](https://discourse.julialang.org/t/could-pop-dict-key-default-have-a-do-block-method/55378/3 "2021-02-16T09:49:09Z")

</div>

Why not use this pattern:

```julia
a = Dict("x" => 3, "y" => 4)

v = pop!(a, "x", nothing)
if v === nothing
    sleep(10)
    v = 5
end

```

IMO it would be a bit strange for `pop!` to accept a function like this since that function doesn’t need access to the object `a` (e.g. to add a key like the `get!` version that accepts a function). There is just no reason to move that code block inside `pop!` when it could just as well be outside.

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [February 16, 2021, 9:57am UTC](https://discourse.julialang.org/t/could-pop-dict-key-default-have-a-do-block-method/55378/4 "2021-02-16T09:57:07Z")

</div>

This certainly works, and I guess this would not have type-inference impacts as such because of Union splitting?

---

<div class="post-metadata">

**Author:** ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)\
**Post date:** [February 16, 2021, 10:10am UTC](https://discourse.julialang.org/t/could-pop-dict-key-default-have-a-do-block-method/55378/5 "2021-02-16T10:10:22Z")

</div>

That code would fail though on `Dict("x" => nothing, "y" => 4)`, and to truly handle that right without sacrificing performance, I think you basically have to write out something similar to the do-block `get!` function, so from that angle I think it could be a useful addition to have it for `pop!` too.
