Skip to content

Commit

Permalink
Add doctest for IdDict with example (#38881)
Browse files Browse the repository at this point in the history
This came up from a Slack discussion and was based on a surprising [Python behavior](https://twitter.com/DahlitzF/status/1338384990040682498).
@haampie provided the example and I thought the docstrings could help out another beginner to understand why a different data structure is justified.
  • Loading branch information
miguelraz authored Dec 15, 2020
1 parent 4dede6d commit bd0ddf3
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion base/iddict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,22 @@
`IdDict{K,V}()` constructs a hash table using object-id as hash and
`===` as equality with keys of type `K` and values of type `V`.
See [`Dict`](@ref) for further help.
See [`Dict`](@ref) for further help. In the example below, The `Dict`
keys are all `isequal` and therefore get hashed the same, so they get overwritten.
The `IdDict` hashes by object-id, and thus preserves the 3 different keys.
# Examples
```julia-repl
julia> Dict(true => "yes", 1 => "no", 1.0 => "maybe")
Dict{Real, String} with 1 entry:
1.0 => "maybe"
julia> IdDict(true => "yes", 1 => "no", 1.0 => "maybe")
IdDict{Any, String} with 3 entries:
true => "yes"
1.0 => "maybe"
1 => "no"
```
"""
mutable struct IdDict{K,V} <: AbstractDict{K,V}
ht::Vector{Any}
Expand Down

0 comments on commit bd0ddf3

Please sign in to comment.