# Get vs get!

**URL:** <https://discourse.julialang.org/t/get-vs-get/122985>\
**Category:** New to Julia\
**Created:** [November 23, 2024, 9:00am UTC](https://discourse.julialang.org/t/get-vs-get/122985 "2024-11-23T09:00:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![martin\_sanchez](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/martin_sanchez/32/207525_2.png) [@martin\_sanchez](https://discourse.julialang.org/u/martin_sanchez)\
**Post date:** [November 23, 2024, 9:00am UTC](https://discourse.julialang.org/t/get-vs-get/122985/1 "2024-11-23T09:00:06Z")

</div>

I do not know what could get! do to an object. Shouldn’t a get function be non-transformative of the input? I am accesing a value…

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [November 23, 2024, 9:05am UTC](https://discourse.julialang.org/t/get-vs-get/122985/2 "2024-11-23T09:05:02Z")

</div>

Let’s compare by using help mode (you enter by typing a leading `?` and exit by backspacing it):

```julia
help?> get
search: get get! let Set getkey Text getpid Meta

  get(collection, key, default)

  Return the value stored for the given key, or the given default value if no mapping for the key is present.

```

vs

```julia
help?> get!
search: get! get getkey let Set put! getpid Meta

  get!(collection, key, default)

  Return the value stored for the given key, or if no mapping for the key is present, store key => default, and return default.

```

`get!` has the extra step of storing the default. Could have been called something like `get_maybe_set_default!`, but it’s more typing and would make it unfair to omit the default behavior from `get_maybe_default`. `get!` is enough of a hint if you already know what `get` does (and it’s not the same as `getindex` for brackets syntax).

---

<div class="post-metadata">

**Author:** ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)\
**Post date:** [November 23, 2024, 11:22am UTC](https://discourse.julialang.org/t/get-vs-get/122985/3 "2024-11-23T11:22:27Z")

</div>

Especially the method computing the default value

```julia
get!(f::Union{Function, Type}, collection, key)

```

is super handy for memoization:

```julia-repl
julia> function slowfib(n::Integer)
           n < 2 ? 1 : slowfib(n-1) + slowfib(n-2)
       end

julia> function fastfib(n::T; cache = Dict{T,T}()) where {T <: Integer}
           get!(cache, n) do
               n < 2 ? 1 : fastfib(n-1; cache=cache) + fastfib(n-2; cache = cache)
           end
       end

julia> fastfib.(BigInt.(1:100)) # Don't try this with slowfib!
100-element Vector{BigInt}:
                     1
                     2
                     3
                     5
                     8

```

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [November 23, 2024, 11:53am UTC](https://discourse.julialang.org/t/get-vs-get/122985/4 "2024-11-23T11:53:49Z")

</div>

> [@martin\_sanchez](#):
>
> Shouldn’t a get function be non-transformative of the input?

That’s a reasonable expectation and is true for `get`. On the other hand `get!` ends with an exclamation mark, which in Julia is a naming convention for functions that change their input in some way.
