# get(::Function, ::Dict, ::Key) not defined for Base.ImmutableDict?

**URL:** https://discourse.julialang.org/t/get-function-dict-key-not-defined-for-base-immutabledict/58897
**Category:** General Usage
**Created:** [April 9, 2021, 1:52am UTC](https://discourse.julialang.org/t/get-function-dict-key-not-defined-for-base-immutabledict/58897 "2021-04-09T01:52:28Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![murari0](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/murari0/32/23792_2.png) [@murari0](https://discourse.julialang.org/u/murari0)
#### Post date: [April 9, 2021, 1:52am UTC](https://discourse.julialang.org/t/get-function-dict-key-not-defined-for-base-immutabledict/58897/1 "2021-04-09T01:52:28Z")

</div>

While playing around with `Dict`s, I found that I was not able to use the do-block syntax, e.g.

```julia
get(a, key) do
    @warn "Key not found. Falling back to default"
    default
end

```

when `a` is of type `Base.ImmutableDict` instead of `Dict`. Is there a reason it isn’t implemented, and is there a workaround I can use instead? (I guess I could make a custom version of the function by looking at [dict.jl on GitHub](https://github.com/JuliaLang/julia/blob/f021c67fe9aba889f015b66c6d5bf6394762c60d/base/dict.jl#L787) though I’m not sure that’s the best idea)

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [April 9, 2021, 2:08am UTC](https://discourse.julialang.org/t/get-function-dict-key-not-defined-for-base-immutabledict/58897/2 "2021-04-09T02:08:00Z")

</div>

Seems like an oversight (a missing fallback method for `AbstractDict`), since the documentation lists `get(f::Function, collection, key)` as a generic method for any collection. Maybe file an issue?

As a workaround, you could define something like

```julia
struct _KeyNotFound; end
function myget(default::Base.Callable, collection, key)
    result = get(collection, key, _KeyNotFound())
    return result isa _KeyNotFound ? default() : result
end

```

---

<div class="post-metadata">

### Author: ![murari0](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/murari0/32/23792_2.png) [@murari0](https://discourse.julialang.org/u/murari0)
#### Post date: [April 9, 2021, 3:22am UTC](https://discourse.julialang.org/t/get-function-dict-key-not-defined-for-base-immutabledict/58897/3 "2021-04-09T03:22:35Z")

</div>

Thank you, that is a more elegant workaround than what I had come up with! I will file that as an issue as well.
