We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent e5d6a68 commit 4c7fbc0Copy full SHA for 4c7fbc0
3 files changed
src/containers.jl
@@ -27,9 +27,12 @@ macro make_container(margs...)
27
definitions = []
28
argss = []
29
for a in margs
30
- push!(argss, a isa Expr && a.head == :vect ? # optional
31
- [nothing, a.args[1]] :
32
- [a])
+ push!(argss,
+ a isa Expr && a.head == :vect ? # optional
+ [nothing, a.args[1]] :
33
+ a isa Expr && a.head == :call && a.args[1] == :(=>) ? # curly
34
+ [a.args[2] => a.args[3]] :
35
+ [a])
36
37
end
38
pushfirst!(argss, [(), :X, :(::Type{X}) => :X]) # for Sampler
@@ -65,6 +68,7 @@ end
65
68
# Tuple as a container
66
69
@make_container(T::Type{<:Tuple})
67
70
@make_container(::Type{Tuple}, n::Integer)
71
+@make_container(T::Type{NTuple{N,TT} where N} => TT, n::Integer)
72
73
## arrays (same as in Random, but with explicit type specification, e.g. rand(Int, Array, 4)
74
src/sampling.jl
@@ -197,6 +197,11 @@ make(::Type{Tuple}, ::Type{X}, n::Integer) where {X} = make(NTuple{Int(n)}, X)
197
198
make(::Type{Tuple}, n::Integer) = make(Tuple, default_sampling(Tuple), Int(n))
199
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
+
205
# disambiguate
206
207
make(::Type{T}, X) where {T<:Tuple} = _make(T, X)
test/runtests.jl
@@ -191,6 +191,12 @@ const spString = Sampler(MersenneTwister, String)
191
192
s = rand(rng..., NTuple{3})
193
@test s isa NTuple{3,Float64}
194
195
+ s = rand(rng..., NTuple{N,UInt8} where N, 3)
196
+ @test s isa NTuple{3,UInt8}
+ s = rand(rng..., 1:3, NTuple{N,UInt8} where N, 3)
+ @test all(in(1:3), s)
@testset "Rand" for rng in ([], [MersenneTwister(0)], [RandomDevice()])
@@ -309,6 +315,14 @@ end
309
315
310
316
r = rand(make(NTuple{3}))
311
317
@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
323
+ @test all(in(1:3), r)
324
+ r = rand(make(NTuple{N,UInt8} where N, UInt8, 3))
325
312
326
313
327
314
328
@testset "rand(make(String, ...))" begin
0 commit comments