Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide a better example for get!(f::Function, collection, key) #36964

Merged
merged 1 commit into from
Aug 11, 2020
Merged

Provide a better example for get!(f::Function, collection, key) #36964

merged 1 commit into from
Aug 11, 2020

Conversation

CameronBieganek
Copy link
Contributor

I've needed get!(f::Function, collection, key) in the past, but the example given in the docstring was confusing or even actively misleading. So I didn't realize that get!(f::Function, collection, key) was in fact the right function. Instead, I ended up writing the naive code like this:

if haskey(d, k)
    return d[k]
else
    return d[k] = foo(a)
end

So, I've changed the example for get!(f::Function, collection, key). I think the new example better motivates the usage of get!.

The example for get(f::Function, collection, key) (the non-mutating version) should probably also be updated. However, I'm actually not sure of a good example for the case where you don't update the dictionary.

I've needed `get!(f::Function, collection, key)` in the past, but the example given in the docstring was confusing or even actively misleading. So I didn't realize that `get!(f::Function, collection, key)` was in fact the right function. Instead, I ended up writing the naive code like this:

```julia
if haskey(d, k)
    return d[k]
else
    return d[k] = foo(a)
end
```

So, I've changed the example for `get!(f::Function, collection, key)`. I think the new example better motivates the usage of `get!`.

The example for `get(f::Function, collection, key)` (the non-mutating version) should probably also be updated. However, I'm actually not sure of a good example for the case where you don't update the dictionary.
@fredrikekre fredrikekre merged commit 894ee04 into JuliaLang:master Aug 11, 2020
@fredrikekre fredrikekre added system:32-bit Affects only 32-bit systems domain:docs This change adds or pertains to documentation and removed system:32-bit Affects only 32-bit systems labels Aug 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain:docs This change adds or pertains to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants