Skip to content

Commit e5d6a68

Browse files
committed
enable rand(NTuple{N})::NTuple{N,Float})
1 parent 967110b commit e5d6a68

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

src/sampling.jl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,13 @@ end
157157
# and make(NTuple{N}, S)
158158

159159
@generated function _make(::Type{T}, args...) where T <: Tuple
160-
isempty(args) && return :(Make0{$(T === Tuple ? Tuple{} : T)}())
160+
if isempty(args)
161+
TT = T === Tuple ? Tuple{} :
162+
T === NTuple ? Tuple{} :
163+
T isa UnionAll && Type{T} <: Type{NTuple{N}} where N ? T{default_sampling(Tuple)} :
164+
T
165+
return :(Make0{$TT}())
166+
end
161167
isNT = length(args) == 1 && T !== Tuple && (
162168
T <: NTuple || !isa(T, UnionAll)) # !isa(Tuple, UnionAll) !!
163169

test/runtests.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,9 @@ const spString = Sampler(MersenneTwister, String)
188188
@test all(in(1:3), s)
189189
s = rand(rng..., Tuple, 4)
190190
@test s isa NTuple{4,Float64}
191+
192+
s = rand(rng..., NTuple{3})
193+
@test s isa NTuple{3,Float64}
191194
end
192195

193196
@testset "Rand" for rng in ([], [MersenneTwister(0)], [RandomDevice()])
@@ -303,6 +306,9 @@ end
303306
r = rand(make(Tuple{AbstractFloat,Integer}, 1:3))
304307
@test r isa Tuple{Float64,Int64}
305308
@test all(in(1:3), r)
309+
310+
r = rand(make(NTuple{3}))
311+
@test r isa NTuple{3,Float64}
306312
end
307313

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

0 commit comments

Comments
 (0)