Skip to content

Commit b30afaa

Browse files
committed
Pair API: enable recursive pairs
1 parent 38beaf5 commit b30afaa

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

src/sampling.jl

+11-8
Original file line numberDiff line numberDiff line change
@@ -567,16 +567,19 @@ end
567567

568568
# this is experimental
569569

570-
@inline Sampler(RNG::Type{<:AbstractRNG}, (a, b)::Pair{<:Union{DataType,UnionAll}},
571-
r::Repetition) =
570+
pair_to_make((a, b)::Pair) =
572571
b isa Tuple ?
573-
Sampler(RNG, make(a, b...), r) :
574-
Sampler(RNG, make(a, b), r)
572+
make(a, map(pair_to_make, b)...) :
573+
make(a, pair_to_make(b))
574+
575+
pair_to_make(x) = x
576+
577+
@inline Sampler(RNG::Type{<:AbstractRNG}, p::Pair{<:Union{DataType,UnionAll}},
578+
r::Repetition) =
579+
Sampler(RNG, pair_to_make(p), r)
575580

576581
# nothing can be inferred when only the pair type is available
577582
@inline gentype(::Type{<:Pair{<:Union{DataType,UnionAll}}}) = Any
578583

579-
@inline gentype((a, b)::Pair{<:Union{DataType,UnionAll}}) =
580-
b isa Tuple ?
581-
gentype(make(a, b...)) :
582-
gentype(make(a, b))
584+
@inline gentype(p::Pair{<:Union{DataType,UnionAll}}) =
585+
gentype(pair_to_make(p))

test/runtests.jl

+3
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,9 @@ end
572572

573573
@test rand(Complex => Int, 3) isa Vector{Complex{Int}}
574574
@test rand(Pair => (String, Int8), Set, 3) isa Set{Pair{String,Int8}}
575+
576+
nt = rand(NTuple{4} => Complex => 1:3)
577+
@test nt isa NTuple{4,Complex{Int64}}
575578
end
576579

577580

0 commit comments

Comments
 (0)