Skip to content

Commit

Permalink
Pair API: allow any type of Pair
Browse files Browse the repository at this point in the history
Do not restrict the first argument to be a `DataType`/`UnionAll`, as
`make` is about to start accepting non-type objects as first argument.
  • Loading branch information
rfourquet committed Sep 10, 2020
1 parent c15362d commit 71899e8
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 7 deletions.
10 changes: 4 additions & 6 deletions src/sampling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -574,12 +574,10 @@ pair_to_make((a, b)::Pair) =

pair_to_make(x) = x

@inline Sampler(RNG::Type{<:AbstractRNG}, p::Pair{<:Union{DataType,UnionAll}},
r::Repetition) =
Sampler(RNG, pair_to_make(p), r)
@inline Sampler(RNG::Type{<:AbstractRNG}, p::Pair, r::Repetition) =
Sampler(RNG, pair_to_make(p), r)

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

@inline gentype(p::Pair{<:Union{DataType,UnionAll}}) =
gentype(pair_to_make(p))
@inline gentype(p::Pair) = gentype(pair_to_make(p))
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ end
@testset "rand(T => x) & rand(T => (x, y, ...))" begin
@test rand(Complex => Int) isa Complex{Int}
@test rand(Pair => (String, Int8)) isa Pair{String,Int8}
@test_throws ArgumentError rand(1=>2) # should not call rand(make(1, 2))
@test_throws MethodError rand(1=>2) # calls rand(make(1, 2))

@test rand(Complex => Int, 3) isa Vector{Complex{Int}}
@test rand(Pair => (String, Int8), Set, 3) isa Set{Pair{String,Int8}}
Expand Down

0 comments on commit 71899e8

Please sign in to comment.