Skip to content

Commit 4c7fbc0

Browse files
committed
enable make(NTuple{N,T} where N, [X], n::Integer) where T
1 parent e5d6a68 commit 4c7fbc0

3 files changed

Lines changed: 26 additions & 3 deletions

File tree

src/containers.jl

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,12 @@ macro make_container(margs...)
2727
definitions = []
2828
argss = []
2929
for a in margs
30-
push!(argss, a isa Expr && a.head == :vect ? # optional
31-
[nothing, a.args[1]] :
32-
[a])
30+
push!(argss,
31+
a isa Expr && a.head == :vect ? # optional
32+
[nothing, a.args[1]] :
33+
a isa Expr && a.head == :call && a.args[1] == :(=>) ? # curly
34+
[a.args[2] => a.args[3]] :
35+
[a])
3336

3437
end
3538
pushfirst!(argss, [(), :X, :(::Type{X}) => :X]) # for Sampler
@@ -65,6 +68,7 @@ end
6568
# Tuple as a container
6669
@make_container(T::Type{<:Tuple})
6770
@make_container(::Type{Tuple}, n::Integer)
71+
@make_container(T::Type{NTuple{N,TT} where N} => TT, n::Integer)
6872

6973
## arrays (same as in Random, but with explicit type specification, e.g. rand(Int, Array, 4)
7074

src/sampling.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,11 @@ make(::Type{Tuple}, ::Type{X}, n::Integer) where {X} = make(NTuple{Int(n)}, X)
197197

198198
make(::Type{Tuple}, n::Integer) = make(Tuple, default_sampling(Tuple), Int(n))
199199

200+
# NTuple{N,T} where N
201+
make(::Type{NTuple{N,T} where N}, n::Integer) where {T} = make(NTuple{Int(n),T})
202+
make(::Type{NTuple{N,T} where N}, X, n::Integer) where {T} = make(NTuple{Int(n),T}, X)
203+
make(::Type{NTuple{N,T} where N}, ::Type{X}, n::Integer) where {T,X} = make(NTuple{Int(n),T}, X)
204+
200205
# disambiguate
201206

202207
make(::Type{T}, X) where {T<:Tuple} = _make(T, X)

test/runtests.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,12 @@ const spString = Sampler(MersenneTwister, String)
191191

192192
s = rand(rng..., NTuple{3})
193193
@test s isa NTuple{3,Float64}
194+
195+
s = rand(rng..., NTuple{N,UInt8} where N, 3)
196+
@test s isa NTuple{3,UInt8}
197+
s = rand(rng..., 1:3, NTuple{N,UInt8} where N, 3)
198+
@test s isa NTuple{3,UInt8}
199+
@test all(in(1:3), s)
194200
end
195201

196202
@testset "Rand" for rng in ([], [MersenneTwister(0)], [RandomDevice()])
@@ -309,6 +315,14 @@ end
309315

310316
r = rand(make(NTuple{3}))
311317
@test r isa NTuple{3,Float64}
318+
319+
r = rand(make(NTuple{N,UInt8} where N, 3))
320+
@test r isa NTuple{3,UInt8}
321+
r = rand(make(NTuple{N,UInt8} where N, 1:3, 3))
322+
@test r isa NTuple{3,UInt8}
323+
@test all(in(1:3), r)
324+
r = rand(make(NTuple{N,UInt8} where N, UInt8, 3))
325+
@test r isa NTuple{3,UInt8}
312326
end
313327

314328
@testset "rand(make(String, ...))" begin

0 commit comments

Comments
 (0)