Skip to content

Commit 09ea9b4

Browse files
committed
Dict generation: use fieldtype instead of .parameters
1 parent 62d060e commit 09ea9b4

2 files changed

Lines changed: 13 additions & 2 deletions

File tree

src/containers.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ end
2020

2121
rand!(rng::AbstractRNG, A::AbstractDict{K,V}, sp::Sampler) where {K,V} = _rand!(rng, A, length(A), sp)
2222

23-
rand(rng::AbstractRNG, dist::Distribution{<:Pair}, ::Type{T}, n::Integer) where {T<:AbstractDict} =
24-
_rand!(rng, deduce_type(T, eltype(dist).parameters...)(), n, Sampler(rng, dist))
23+
rand(rng::AbstractRNG, dist::Distribution{P}, ::Type{T}, n::Integer) where {P<:Pair,T<:AbstractDict} =
24+
_rand!(rng, deduce_type(T, fieldtype(P, 1), fieldtype(P, 2))(), n, Sampler(rng, dist))
2525

2626
rand(u::Distribution{<:Pair}, ::Type{T}, n::Integer) where {T<:AbstractDict} = rand(GLOBAL_RNG, u, T, n)
2727

test/runtests.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,14 @@ end
102102
@test collect(Iterators.take(d, 10)) isa Vector{Int}
103103
end
104104
end
105+
106+
struct PairDistrib <: RandomExtensions.Distribution{Pair}
107+
end
108+
109+
Random.rand(rng::AbstractRNG, ::Random.SamplerTrivial{PairDistrib}) = 1=>2
110+
111+
@testset "allow abstract Pair when generating a Dict" begin
112+
d = rand(PairDistrib(), Dict, 1)
113+
@test d == Dict(1=>2)
114+
@test typeof(d) == Dict{Any,Any}
115+
end

0 commit comments

Comments
 (0)