Skip to content

Commit 0230564

Browse files
authored
Merge pull request JuliaCollections#730 from mbauman/mb/default-dict-docs
Document Default*Dict and add Default*Dict{K,V} constructors
2 parents 81cc5ed + 0e290ba commit 0230564

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

Project.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ name = "DataStructures"
22
uuid = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
33
version = "0.19.0-DEV"
44

5-
65
[deps]
76
Compat = "34da2185-b29b-5c13-b0c7-acf172513d20"
87
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"

src/default_dict.jl

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,19 @@ end
8585
for _Dict in [:Dict, :OrderedDict]
8686
DefaultDict = Symbol("Default"*string(_Dict))
8787
@eval begin
88+
"""
89+
$($DefaultDict)(default, pairs...; passkey=false)
90+
$($DefaultDict)(default, d::AbstractDict; passkey=false)
91+
$($DefaultDict){K, V}(default, pairs...; passkey=false)
92+
$($DefaultDict){K, V}(default, dict::AbstractDict; passkey=false)
93+
94+
Construct an $($(_Dict == :Dict ? "un" : ""))ordered dictionary from the given key-value `pairs` or `dict`
95+
with a `default` value to be returned for keys that are not stored in the dictionary. The key and value
96+
types may be optionally specified with the `K` and `V` parameters.
97+
98+
If the `default` value is a `Function` or `Type`, then it will be _called_ upon the retrieval of a missing key,
99+
with either 0 arguments (if `passkey==false`) or with the key that was requested.
100+
"""
88101
struct $DefaultDict{K,V,F} <: AbstractDict{K,V}
89102
d::DefaultDictBase{K,V,F,$_Dict{K,V}}
90103

@@ -113,7 +126,8 @@ for _Dict in [:Dict, :OrderedDict]
113126

114127
# Constructor syntax: DefaultDictBase{Int,Float64}(default)
115128
$DefaultDict{K,V}(; kwargs...) where {K,V} = throw(ArgumentError("$DefaultDict: no default specified"))
116-
$DefaultDict{K,V}(default::F; kwargs...) where {K,V,F} = $DefaultDict{K,V,F}(default; kwargs...)
129+
$DefaultDict{K,V}(default::F, pairs...; kwargs...) where {K,V,F} = $DefaultDict{K,V,F}(default, $_Dict{K,V}(pairs...); kwargs...)
130+
$DefaultDict{K,V}(default::F, d::AbstractDict; kwargs...) where {K,V,F} = $DefaultDict{K,V,F}(default, $_Dict{K,V}(d); kwargs...)
117131

118132
## Functions
119133

test/test_default_dict.jl

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import DataStructures: DefaultDictBase
1+
import DataStructures: DefaultDictBase, OrderedDict
22

33
@testset "DefaultDict" begin
44

@@ -26,6 +26,14 @@ import DataStructures: DefaultDictBase
2626
@test_throws ArgumentError DefaultDict{AbstractString, Int}()
2727

2828
@test isa(DefaultDict(0.0, 1 => 1.0), DefaultDict{Int, Float64, Float64})
29+
@test isa(DefaultDict(0.0, Dict(1 => 1.0)), DefaultDict{Int, Float64, Float64})
30+
31+
@test isa(DefaultDict{Float64, Int}(0.0), DefaultDict{Float64, Int, Float64})
32+
@test isa(DefaultDict{Float64, Int}(0.0, Dict()), DefaultDict{Float64, Int, Float64})
33+
@test isa(DefaultDict{Float64, Int}(0.0, OrderedDict()), DefaultDict{Float64, Int, Float64})
34+
@test isa(DefaultDict{Float64, Int}(0.0, 1 => 1.0), DefaultDict{Float64, Int, Float64})
35+
@test isa(DefaultDict{Float64, Int}(0.0, Dict(1 => 1.0)), DefaultDict{Float64, Int, Float64})
36+
@test isa(DefaultDict{Float64, Int}(0.0, OrderedDict(1 => 1.0)), DefaultDict{Float64, Int, Float64})
2937
end
3038

3139
@testset "Core Functionality" begin
@@ -132,7 +140,18 @@ import DataStructures: DefaultDictBase
132140
@testset "DefaultOrderedDict" begin
133141
@testset "construction" begin
134142
@test_throws ArgumentError DefaultOrderedDict()
143+
@test_throws ArgumentError DefaultOrderedDict(AbstractString, Int)
135144
@test_throws ArgumentError DefaultOrderedDict{AbstractString, Int}()
145+
146+
@test isa(DefaultOrderedDict(0.0, 1 => 1.0), DefaultOrderedDict{Int, Float64, Float64})
147+
@test isa(DefaultOrderedDict(0.0, Dict(1 => 1.0)), DefaultOrderedDict{Int, Float64, Float64})
148+
149+
@test isa(DefaultOrderedDict{Float64, Int}(0.0), DefaultOrderedDict{Float64, Int, Float64})
150+
@test isa(DefaultOrderedDict{Float64, Int}(0.0, Dict()), DefaultOrderedDict{Float64, Int, Float64})
151+
@test isa(DefaultOrderedDict{Float64, Int}(0.0, OrderedDict()), DefaultOrderedDict{Float64, Int, Float64})
152+
@test isa(DefaultOrderedDict{Float64, Int}(0.0, 1 => 1.0), DefaultOrderedDict{Float64, Int, Float64})
153+
@test isa(DefaultOrderedDict{Float64, Int}(0.0, Dict(1 => 1.0)), DefaultOrderedDict{Float64, Int, Float64})
154+
@test isa(DefaultOrderedDict{Float64, Int}(0.0, OrderedDict(1 => 1.0)), DefaultOrderedDict{Float64, Int, Float64})
136155
end
137156

138157
@testset "Core Functionality" begin

0 commit comments

Comments
 (0)