Skip to content

Commit d1477af

Browse files
committed
use gentype instead of eltype
This allows stricter typing for few containers.
1 parent 68cd04c commit d1477af

4 files changed

Lines changed: 15 additions & 6 deletions

File tree

src/RandomExtensions.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module RandomExtensions
22

33
export Combine, Uniform, Normal, Exponential, CloseOpen, Rand
44

5-
import Random: Sampler, rand, rand!
5+
import Random: Sampler, rand, rand!, gentype
66

77
using Random
88
using Random: GLOBAL_RNG, SamplerTrivial, SamplerSimple, SamplerTag, Repetition

src/containers.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ rand!(rng::AbstractRNG, A::AbstractSet, sp::Sampler) = _rand!(rng, A, length(A),
4444
rand(r::AbstractRNG, ::Type{T}, n::Integer) where {T<:AbstractSet} = rand(r, Float64, T, n)
4545
rand( ::Type{T}, n::Integer) where {T<:AbstractSet} = rand(GLOBAL_RNG, T, n)
4646

47-
rand(r::AbstractRNG, X, ::Type{T}, n::Integer) where {T<:AbstractSet} = _rand0!(r, deduce_type(T, eltype(X))(), n, X)
47+
rand(r::AbstractRNG, X, ::Type{T}, n::Integer) where {T<:AbstractSet} = _rand0!(r, deduce_type(T, gentype(X))(), n, X)
4848
rand( X, ::Type{T}, n::Integer) where {T<:AbstractSet} = rand(GLOBAL_RNG, X, T, n)
4949

5050
rand(r::AbstractRNG, ::Type{X}, ::Type{T}, n::Integer) where {X,T<:AbstractSet} = _rand0!(r, deduce_type(T, X)(), n, X)
@@ -70,7 +70,7 @@ rand(r::AbstractRNG, ::Type{X}, p::AbstractFloat, m::Integer) where {X} =
7070
rand(X, p::AbstractFloat, m::Integer) = rand(GLOBAL_RNG, X, p, m)
7171

7272
rand(r::AbstractRNG, X::Sampler, p::AbstractFloat, m::Integer, n::Integer) =
73-
sprand(r, m, n, p, (r, n)->rand(r, X, n), eltype(X))
73+
sprand(r, m, n, p, (r, n)->rand(r, X, n), gentype(X))
7474

7575
rand(r::AbstractRNG, X, p::AbstractFloat, m::Integer, n::Integer) =
7676
rand(r, Sampler(r, X), p, m, n)

src/distributions.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ Combine(::Type{T}, ::Type{X}, y::Y) where {T,X,Y} = Combine2{deduce_type(T,X,Y),
3030
Combine(::Type{T}, x::X, ::Type{Y}) where {T,X,Y} = Combine2{deduce_type(T,X,Y),X,Type{Y}}(x, Y)
3131
Combine(::Type{T}, ::Type{X}, ::Type{Y}) where {T,X,Y} = Combine2{deduce_type(T,X,Y),Type{X},Type{Y}}(X, Y)
3232

33-
deduce_type(::Type{T}, ::Type{X}, ::Type{Y}) where {T,X,Y} = _deduce_type(T, Val(isconcretetype(T)), eltype(X), eltype(Y))
34-
deduce_type(::Type{T}, ::Type{X}) where {T,X} = _deduce_type(T, Val(isconcretetype(T)), eltype(X))
33+
deduce_type(::Type{T}, ::Type{X}, ::Type{Y}) where {T,X,Y} = _deduce_type(T, Val(isconcretetype(T)), gentype(X), gentype(Y))
34+
deduce_type(::Type{T}, ::Type{X}) where {T,X} = _deduce_type(T, Val(isconcretetype(T)), gentype(X))
3535

3636
_deduce_type(::Type{T}, ::Val{true}, ::Type{X}, ::Type{Y}) where {T,X,Y} = T
3737
_deduce_type(::Type{T}, ::Val{false}, ::Type{X}, ::Type{Y}) where {T,X,Y} = deduce_type(T{X}, Y)
@@ -55,7 +55,7 @@ struct UniformWrap{T,E} <: Uniform{E}
5555
val::T
5656
end
5757

58-
Uniform(x::T) where {T} = UniformWrap{T,eltype(T)}(x)
58+
Uniform(x::T) where {T} = UniformWrap{T,gentype(T)}(x)
5959

6060
Base.getindex(x::UniformWrap) = x.val
6161

test/runtests.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,12 @@ Random.rand(rng::AbstractRNG, ::Random.SamplerTrivial{PairDistrib}) = 1=>2
113113
@test d == Dict(1=>2)
114114
@test typeof(d) == Dict{Any,Any}
115115
end
116+
117+
@testset "some tight typing" begin
118+
UI = Random.UInt52()
119+
@test eltype(rand(MersenneTwister(), Random.Sampler(MersenneTwister, UI), .6, 1, 0)) == UInt64
120+
@test eltype(rand(UI, Set, 3)) == UInt64
121+
@test eltype(rand(Uniform(UI), 3)) == UInt64
122+
a = rand(RandomExtensions.Combine(Pair, Int, UI))
123+
@test fieldtype(typeof(a), 2) == UInt64
124+
end

0 commit comments

Comments
 (0)