Skip to content

Commit cf56281

Browse files
committed
dict.jl: clarify ImmutableDict comments
1 parent 2cb02a1 commit cf56281

File tree

1 file changed

+47
-7
lines changed

1 file changed

+47
-7
lines changed

base/dict.jl

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -776,20 +776,60 @@ struct ImmutableDict{K,V} <: AbstractDict{K,V}
776776
end
777777

778778
"""
779-
ImmutableDict
779+
ImmutableDict(key=>value, key=>value, ...)
780780
781-
`ImmutableDict` is a dictionary implemented as an immutable linked list,
782-
which is optimal for small dictionaries that are constructed over many individual insertions.
783-
Note that it is not possible to remove a value, although it can be partially overridden and hidden
784-
by inserting a new value with the same key.
781+
`ImmutableDict{K,V}()`
785782
786-
ImmutableDict(KV::Pair)
783+
`ImmutableDict` is a dictionary implemented as an immutable linked
784+
list, used for small mappings with a few entries where the overhead
785+
of accessing a hash table is higher than that of a linear search of a
786+
linked list.
787787
788-
Create a new entry in the `ImmutableDict` for a `key => value` pair
788+
Base.ImmutableDict(key=>value, key=>value, ...)
789+
790+
`Base.ImmutableDict(key=>value, key=>value, ...)` constructs a linked
791+
list; the first argument sets the type of the keys and values. Keys
792+
are compared with `isequal`.
793+
794+
`Base.ImmutableDict{K,V}()` constructs an empty list with keys of type
795+
K and values of type V.
796+
797+
Base.ImmutableDict(imdict, key=>value, key=>value, ...)
798+
799+
`Base.ImmutableDict(imdict, key=>value, key=>value, ...)` constructs a
800+
new `ImmutableDict` from an existing `ImmutableDict` and additional KV
801+
pairs, returning a new list. The original `ImmutableDict` becomes the
802+
tail of the new list.
803+
804+
Pairs cannot be removed from an immutable dictionary, but they can be
805+
shadowed by adding an additional pair with a duplicate key; indexing
806+
operations will find the the last key added.
789807
790808
- use `(key => value) in dict` to see if this particular combination is in the properties set
791809
- use `get(dict, key, default)` to retrieve the most recent value for a particular key
792810
811+
# Examples
812+
813+
julia> ctypes = Base.ImmutableDict("char"=>:char, "int"=>:int)
814+
Base.ImmutableDict{String, Symbol} with 2 entries:
815+
"int" => :int
816+
"char" => :char
817+
818+
julia> ctypes = Base.ImmutableDict(ctypes, "float"=>:float, "double"=>:double)
819+
Base.ImmutableDict{String, Symbol} with 4 entries:
820+
"double" => :double
821+
"float" => :float
822+
"int" => :int
823+
"char" => :char
824+
825+
julia> viewparameters = Base.ImmutableDict{String,Any}()
826+
Base.ImmutableDict{String, Any}()
827+
828+
julia> viewparameters = Base.ImmutableDict(
829+
viewparameters, "type"=>"perspective", "direction"=>(0, 0, 1))
830+
Base.ImmutableDict{String, Any} with 2 entries:
831+
"direction" => (0, 0, 1)
832+
"type" => "perspective"
793833
"""
794834
ImmutableDict
795835
ImmutableDict(KV::Pair{K,V}) where {K,V} = ImmutableDict{K,V}(KV[1], KV[2])

0 commit comments

Comments
 (0)