Skip to content

docs: revise and expand ImmutableDict docstring. #58445

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 47 additions & 7 deletions base/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -776,20 +776,60 @@ struct ImmutableDict{K,V} <: AbstractDict{K,V}
end

"""
ImmutableDict
ImmutableDict(key=>value, key=>value, ...)

`ImmutableDict` is a dictionary implemented as an immutable linked list,
which is optimal for small dictionaries that are constructed over many individual insertions.
Note that it is not possible to remove a value, although it can be partially overridden and hidden
by inserting a new value with the same key.
`ImmutableDict{K,V}()`

ImmutableDict(KV::Pair)
`ImmutableDict` is a dictionary implemented as an immutable linked
list, used for small mappings with a few entries where the overhead
of accessing a hash table is higher than that of a linear search of a
linked list.

Create a new entry in the `ImmutableDict` for a `key => value` pair
Base.ImmutableDict(key=>value, key=>value, ...)

`Base.ImmutableDict(key=>value, key=>value, ...)` constructs a linked
list; the first argument sets the type of the keys and values. Keys
are compared with `isequal`.

`Base.ImmutableDict{K,V}()` constructs an empty list with keys of type
K and values of type V.

Base.ImmutableDict(imdict, key=>value, key=>value, ...)

`Base.ImmutableDict(imdict, key=>value, key=>value, ...)` constructs a
new `ImmutableDict` from an existing `ImmutableDict` and additional KV
pairs, returning a new list. The original `ImmutableDict` becomes the
tail of the new list.

Pairs cannot be removed from an immutable dictionary, but they can be
shadowed by adding an additional pair with a duplicate key; indexing
operations will find the the last key added.

- use `(key => value) in dict` to see if this particular combination is in the properties set
- use `get(dict, key, default)` to retrieve the most recent value for a particular key

# Examples

julia> ctypes = Base.ImmutableDict("char"=>:char, "int"=>:int)
Base.ImmutableDict{String, Symbol} with 2 entries:
"int" => :int
"char" => :char

julia> ctypes = Base.ImmutableDict(ctypes, "float"=>:float, "double"=>:double)
Base.ImmutableDict{String, Symbol} with 4 entries:
"double" => :double
"float" => :float
"int" => :int
"char" => :char

julia> viewparameters = Base.ImmutableDict{String,Any}()
Base.ImmutableDict{String, Any}()

julia> viewparameters = Base.ImmutableDict(
viewparameters, "type"=>"perspective", "direction"=>(0, 0, 1))
Base.ImmutableDict{String, Any} with 2 entries:
"direction" => (0, 0, 1)
"type" => "perspective"
"""
ImmutableDict
ImmutableDict(KV::Pair{K,V}) where {K,V} = ImmutableDict{K,V}(KV[1], KV[2])
Expand Down
2 changes: 2 additions & 0 deletions doc/src/base/collections.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ thus may be garbage collected even when referenced in a hash table.
Like `Dict` it uses `hash` for hashing and `isequal` for equality, unlike `Dict` it does
not convert keys on insertion.

[`ImmutableDict`](@ref Base.ImmutableDict) is a linked list implementation.

[`Dict`](@ref)s can be created by passing pair objects constructed with `=>` to a [`Dict`](@ref)
constructor: `Dict("A"=>1, "B"=>2)`. This call will attempt to infer type information from the
keys and values (i.e. this example creates a `Dict{String, Int64}`). To explicitly specify types
Expand Down